我正在使用mysql
数据库。我有三张桌子推车,课程和支票。我需要一行数据,它结合了购物车和课程表,不应该存在于检查表中,我从我的php函数传递userid = $id
。这里的用户ID对于两个表都是相同的。
Courses table:
id | name |
1 | PMP |
2 | CAPM |
3 | Prince2 |
Carts table:
id |userid | username | orderid | exam_id |
1 |13 | Gautham |437b310v671N888M6720 | 1 |
2 |13 | Gautham |437b310v671N888M6720 | 2 |
3 |13 | Gautham |437b310v671N888M6720 | 3 |
4 |14 | Rakesh |678g310v671N888M6720 | 3 |
checks table :
id |userid | username | orderid | exam_id |
1 |13 | Gautham |437b310v671N888M6720 | 1 |
2 |13 | Gautham |437b310v671N888M6720 | 2 |
所以我需要一个结合了购物车和课程表的输出,但数据不应该存在于检查表中,我从我的php函数传递userid = $ id:
1 |13 | Gautham |437b310v671N888M6720 | Prince2 |
2 |14 | Rakesh |678g310v671N888M6720 | Prince2 |
答案 0 :(得分:0)
我自由输入了这个,但它应该给你一个坚实的起点:
SELECT cT.UserID, cT.UserName, cT.OrderID, cS.Name
FROM Carts cT #Carts appears to be the 'base' from which your query originates
INNER JOIN Courses cS ON cT.Exam_ID = cS.ID #Require both Carts & Courses
LEFT JOIN Checks cK ON cT.OrderID = cK.OrderID #Optionally rq Checks
WHERE cK.OrderID IS NULL #but only where there is no check