我是内部联接的新手,我似乎无法弄清楚这里出了什么问题。我希望返回表“events”中的所有行。这是我的表格:
+----+---------+----------+ +----+---------+
| ID | name | venue_id | | ID | name |
+----+---------+----------+ +----+---------+
| 1 | Hub dub | 2 | | 2 | hub dub |
| 2 | Test 2 | 2 | +----+---------+
| 3 | Test 3 | 2 |
| 4 | Test 4 | 2 |
+----+---------+----------+
以下是查询:
SELECT DISTINCT a.*, b.name AS venue a
FROM events b
INNER JOIN venues ON a.venue_id = b.id
WHERE a.name LIKE '%hub%' OR b.name LIKE '%hub%'
出于某种原因,在我更复杂的版本中,只返回事件ID#1。
答案 0 :(得分:1)
语法有点偏。别名(a
)需要在表之后(在列别名之后有它)。
SELECT DISTINCT a.*, b.name AS venue
FROM events b
INNER JOIN venues a ON a.venue_id = b.id
WHERE a.name LIKE '%hub%' OR b.name LIKE '%hub%'
答案 1 :(得分:1)
SELECT DISTINCT a.*, b.name AS venue a
-------------------------------------^
应该是这样的:
SELECT DISTINCT a.*, b.name AS venue
FROM events b
INNER JOIN venues a ON a.venue_id = b.id
WHERE a.name LIKE '%hub%' OR b.name LIKE '%hub%'
它被称为别名。在FROM
之后,它应始终位于表格一侧。
答案 2 :(得分:0)
这样做
SELECT DISTINCT a.*, b.name AS venue
FROM events b
INNER JOIN venues a ON a.venue_id = b.id
WHERE a.name LIKE '%hub%' OR b.name LIKE '%hub%'
答案 3 :(得分:0)
这是你想要的吗?
SELECT DISTINCT a.*, b.name AS venue a
FROM events b
LEFT JOIN venues ON a.venue_id = b.id and b.name LIKE '%hub%'
答案 4 :(得分:0)
结果是正确的,你已经通过venue_id和id连接了两个表,结果是来自表场所的所有行,列名为表事件,之后你用字符串contains%过滤两个表上的列名hub%,结果只是第一行。
PS你错过了桌面场地的标签。
答案 5 :(得分:0)
我认为你想要的是左边加入表A和B.
SELECT a.*, b.name AS venue FROM events b
LEFT JOIN venues a ON a.venue_id = b.id
WHERE a.name LIKE '%hub%' OR b.name LIKE '%hub%'