0

我有两张桌子。

User表有 user_id 和 order_num

Orders表有 order_num、ddmmyyyy、order_detail。

当用户想要查看某个月份的所有订单时,我通常从 user_id 与 cookie 匹配的用户表中选择 order_num,然后在循环和循环内对匹配 order_num 的订单表进行选择,并显示月份是否与所选月份匹配。

有没有更有效的方法来做到这一点?加入某种形式?相当基本的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

    }
4

2 回答 2

2

当然,您可以执行以下操作:

SELECT * FROM users u
JOIN orders o ON u.order_num = o.order_num AND o.mm = ###
WHERE u.user_id = ###
于 2012-05-21T00:38:04.607 回答
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
于 2012-05-21T00:36:08.850 回答