0
我有两个表我想选择所有记录在cardId 2表中的位置,并且如果一个位置被映射到location_card表中,那么地点的名称如何获得表A所有数据与表B只有与表A匹配的行A
它可以通过单独查询然后在脚本中加入它们,但是我想单个查询。我试着用下面的查询,但这个功能工作。
SELECT `cl`.`locationId`, `cl`.`cardId`, `l`.`name`
FROM (`locations` as l)
JOIN `location_cards` as cl ON `l`.`cardId` = `cl`.`cardId`
WHERE `l`.`id` = '2'
location_cards
+---+-------------+-------+
|id | locationId |cardId |
+---+-------------+-------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 2 |
+---+-------------+-------+
locations
+---+-------------+-------+
|id | name |cardId |
+---+-------------+-------+
| 1 | some | 1 |
| 2 | pla | 2 |
| 3 | sdsdffsdf | 2 |
+---+-------------+-------+