duanjuan3931 2012-09-05 22:26
浏览 19
已采纳

MYSQL应该使用2个查询或将它们添加到我的新表中

Ok say i have 1 table for Users it stores USER_NAME, USER_ID, AND USER_COLOR and im making a new table for Logs that will store USER_NAME

when viewing a page and i need to recieve this data i can do 2 querys one for logs and then search users where USER_NAME=USER_NAME

or i can store USER_NAME, USER_ID, AND USER_COLOR all in logs then i can get all the data with just 1 query.

what would be faster or better?

sample query

$sql = "SELECT id, username, level, namecolor FROM users ORDER BY level DESC"; 
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
while($row = mysql_fetch_object($query)) {
    $Member_id = htmlspecialchars($row->id);
  • 写回答

5条回答 默认 最新

  • drra6593 2012-09-05 22:31
    关注

    As you are using MySql, your database is a relational one.

    This gives you (as you already know) many advantages, so, instead of replicating the user information (already stored in its own table) in other table (logs in this case), you'd better to do joins when needing more information about a user.

    Let the users information to be in its own table, so, when querying logs data you may do this:

    SELECT logs.log_id, logs.log_date, logs.user_name, users.user_color
    FROM logs
    LEFT JOIN users ON logs.user_name = users.user_name
    

    Another point: think what would happen when replicating user info, if you add a new field to the user table (email for instance).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行