对于我几乎完成的几个表,我有一个相当复杂的查询。最后一个方面只是汇总一个项目收到的投标数量,但我正在为特定部分中的所有项目执行此操作。查询工作正常,但我尝试将聚合(/ * * /)添加到查询时只返回1行。阅读它似乎我需要一个子查询,除了我不完全确定如何去做。这是迄今为止的查询:
SELECT s.id as section_id, s.name as section, i.id as item_id,
i.title as item_title, item.location_id as item_location_id,
i.slug as item_slug, i.description as item_description,
i.price as item_price, UNIX_TIMESTAMP(i.date_added) as item_date_added,
c.id, c.city, c.particular_id, p.id, p.particular /*,COUNT(o.i_id) as interest*/
FROM section as s
INNER JOIN item as i
ON s.id = i.section_id
INNER JOIN city as c
ON i.location_id = c.id
INNER JOIN particular as p
ON c.particular_id = p.id
/*LEFT JOIN offer as o
ON o.job_id = i.id*/
WHERE s.id = 2
代码的工作原理是它返回我预期的所有行,直到引入/ * * /代码,现在它只返回1行。
你能给我的任何帮助都将不胜感激
由于
强尼
答案 0 :(得分:3)
如果你正在进行聚合,你必须有分组,例如:
SELECT s.name, COUNT(i.id)
FROM section as s
INNER JOIN item as i
GROUP BY s.name
将返回部分名称和在其中找到的项目数。
答案 1 :(得分:1)
它应该可以解决你的问题:
SELECT s.id as section_id, s.name as section, i.id as item_id,
i.title as item_title, item.location_id as item_location_id,
i.slug as item_slug, i.description as item_description,
i.price as item_price, UNIX_TIMESTAMP(i.date_added) as item_date_added,
c.id, c.city, c.particular_id, p.id, p.particular,
(SELECT COUNT(o.i_id) FROM offer AS o ON o.job_id = i.id) AS interest
FROM section as s
INNER JOIN item as i
ON s.id = i.section_id
INNER JOIN city as c
ON i.location_id = c.id
INNER JOIN particular as p
ON c.particular_id = p.id
WHERE s.id = 2