我想知道这里是否有人可以帮我改一下,所以我只能works
来works
id
prod_instruments_id
+ number
+ works_instruments
都在SELECT
`works`.`id`,
`composers`.`first_name`
FROM `works`
JOIN `composers` ON `works`.`composers_id` = `composers`.`id`
JOIN `works_instruments` ON `works`.`id` = `works_instruments`.`works_id`
WHERE
(true)
AND `composers`.`full_name` LIKE '%karl friedrich abel%'
AND `works_instruments`.`prod_dd_instruments_id` = 15
AND `works_instruments`.`number` >= 1
AND `works_instruments`.`prod_dd_instruments_id` = 5
AND `works_instruments`.`number` >= 1
AND `works`.`work_no` <> '001' AND `works`.`short_title` NOT LIKE '%*Works'
ORDER BY
`composers`.`last_name`,
`composers`.`first_name`,
`works`.`full_title`
LIMIT 0,50
?
这就是我所拥有的:
SELECT
`works`.`id`,
`composers`.`first_name`
FROM `works`
JOIN `composers` ON `works`.`composers_id` = `composers`.`id`
JOIN `works_instruments` ON `works`.`id` = `works_instruments`.`works_id`
WHERE
(true)
AND `composers`.`full_name` LIKE '%karl friedrich abel%'
AND `works_instruments`.`prod_dd_instruments_id` = 15
AND `works_instruments`.`number` >= 1
AND `works`.`work_no` <> '001' AND `works`.`short_title` NOT LIKE '%*Works'
ORDER BY
`composers`.`last_name`,
`composers`.`first_name`,
`works`.`full_title`
LIMIT 0,50
现在这不会返回任何结果。 不过这样做:
SELECT
`works`.`id`,
`composers`.`first_name`
FROM `works`
JOIN `composers` ON `works`.`composers_id` = `composers`.`id`
JOIN `works_instruments` ON `works`.`id` = `works_instruments`.`works_id`
WHERE
(true)
AND `composers`.`full_name` LIKE '%karl friedrich abel%'
AND `works_instruments`.`prod_dd_instruments_id` = 5
AND `works_instruments`.`number` >= 1
AND `works`.`work_no` <> '001' AND `works`.`short_title` NOT LIKE '%*Works'
ORDER BY
`composers`.`last_name`,
`composers`.`first_name`,
`works`.`full_title`
LIMIT 0,50
这样做:
SELECT
`works`.`id`,
`composers`.`first_name`
FROM `works`
JOIN `composers` ON `works`.`composers_id` = `composers`.`id`
LEFT JOIN `works_instruments` ON `works`.`id` = `works_instruments`.`works_id`
WHERE
(true)
AND `composers`.`full_name` LIKE '%karl friedrich abel%'
AND `works_instruments`.`prod_dd_instruments_id` = 15
AND `works_instruments`.`number` >= 1
AND `works_instruments`.`prod_dd_instruments_id` = 5
AND `works_instruments`.`number` >= 1
AND `works`.`work_no` <> '001' AND `works`.`short_title` NOT LIKE '%*Works'
ORDER BY
`composers`.`last_name`,
`composers`.`first_name`,
`works`.`full_title`
LIMIT 0,50
你能帮忙吗?
我也试过LEFT和RIGHT加入,但仍然是一个空结果(我只是在这里粘贴左边):
works_instruments
works_instruments
没有uniqe ID是一个问题吗?
在此处查看getline
的屏幕截图:
Screenshot
答案 0 :(得分:0)
没有数字值,其中字段可以同时= 15和= 5。相反,您可以执行&#34; IN(15,5)&#34 ;,按works.id分组,并计算不同的prod_dd_instruments_id值以查看是否有两个;或者使用不同的标准集在两个相同的表中进行两个单独的连接。
修改:根据您在下面提供的其他信息,此类内容可能会更有帮助。
SELECT works.*, composers.*
, SUM(IF(wi.prod_dd_instruments_id=15, wi.number, 0)) AS count15
, SUM(IF(wi.prod_dd_instruments_id=5, wi.number, 0)) AS count5
FROM works
INNER JOIN `composers` ON `works`.`composers_id` = `composers`.`id`
LEFT JOIN `works_instruments` AS wi ON `works`.`id` = wi.`works_id`
WHERE [the other criteria you had]
GROUP BY works.id
HAVING count15 > 1 AND count5 > 1
;
请注意,此查询仅适用于&#34; 5&amp; 15&#34 ;;虽然(从您的&#34; (true)
&#34判断;我猜你正在部分地在代码中构建查询,所以我认为将上面的内容稍微变得不会太难代码更灵活。