dongsu1539 2014-01-24 23:03
浏览 11
已采纳

PHP / MySQL选择查询问题

I was wondering if anyone could give me a hand. I currently have 3 tables as follows:

users(user_id, username, first_name, last_name, password) module(module_id, name, crn) userModule(user_id, module_id)

What would be the best way to access this? From doing basic research it looks like a RIGHT JOIN would be appropriate...

How would I go about looking at the modules table based on the user_id variable?

I gave it ago here but it doesn't seem to work.

$result = mysql_query("SELECT * FROM module JOIN userModule ON (module.module_id = userModule.module_id) JOIN users ON (userModule.user_id = users.user_id) WHERE user_id = 2");

$row = mysql_fetch_row($result);

echo $row[0];
echo $row[1];
echo $row[2];
  • 写回答

1条回答 默认 最新

  • duancan1950 2014-01-24 23:11
    关注
    $result = mysql_query("SELECT * FROM modules LEFT JOIN userModule ON userModule.module_id = modules.module_id WHERE userModule.user_id = 2");
    

    It should work!

    Better yet, if you only want to get modules info:

    SELECT modules.* FROM modules ....
    

    I used LEFT JOIN but there are several other methods : RIGHT JOIN, INNER JOIN or FULL JOIN. See what's the difference between those methods here: http://www.w3schools.com/sql/sql_join.asp

    Also, you should always add " or die(mysql_error());" after your mysql_query(). If the query is wrong (bad formatting, miswritten fields, etc.) it will print a useful error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么