类别
category_id:int
category_name:varchar(50)
记录
tenant_id:int
bulan_tahun:varchar(8)
counter:int
我想使用以下代码加入所有这个表:
SELECT a.tenant_id, a.category_name
FROM (
(tenant INNER JOIN category ON tenant.category_id = category.category_id) AS a
INNER JOIN (
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
) AS b
on a.tenant_id = b.tenant_id
)
但是此代码产生错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a INNER JOIN ( SELECT tenant_id, counter FROM history WHERE bulan_tahun ' at line 3
如果我将子选择分开,它可以完美地工作 对于第一个选择:
SELECT tenant_id, category_name
FROM
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
和第二个选择:
SELECT tenant_id, counter FROM history WHERE
bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')
但如果我一起加入,则会发生错误
任何人都可以帮助我吗?
答案 0 :(得分:1)
问题出在这里..
(tenant INNER JOIN category ON tenant.category_id = category.category_id)
尝试像这样重建整个查询
SELECT a.tenant_id, c.category_name
FROM
tenant t
INNER JOIN
category c
ON ( t.category_id = c.category_id )
INNER JOIN
history h
ON ( t.tenant_id = h.tenant_id )
WHERE
h.bulan_tahun = DATE_FORMAT(CURDATE(), '%m_%Y')