我有两张桌子。
User
表有user_id和order_num
Orders
表包含order_num, dd , mm , yyyy ,order_detail。
当用户想要查看特定月份的所有订单时,我通常从用户表中选择order_num,其中user_id与cookie匹配,然后在循环和循环内执行与订单匹配的订单表上的select,并显示月份是否匹配月份
有更有效的方法吗?有某种联合?相当基本的sql知识,但热衷于扩展
所以,我目前这样做(我知道它不是很好的代码,但逻辑sql这个问题最重要的部分,我知道如何正确编码)
select order_num from user where user_id = $cookie
while (results) {
select order_detail, mm, dd, yyyy where mm = $month
echo order_detail mm dd yyyy
}
答案 0 :(得分:2)
当然,你可以这样做:
SELECT * FROM users u
JOIN orders o ON u.order_num = o.order_num AND o.mm = ###
WHERE u.user_id = ###
答案 1 :(得分:0)
最简单的加入方式,让您入门:
SELECT u.order_num, o.order_detail, o.mm, o.dd, o.yyyy
FROM user AS u, order AS o
WHERE u.order_num = o.order_num AND o.mm = $month