放在哪里'在哪里?

时间:2015-11-06 13:23:12

标签: mysql

我有以下查询:

select 
sites.denomination,
rooms.floor,
rooms.number,
(count(beds.id) - sum(if(beds.state<0,1,0))) as active
from rooms
join sites on rooms.site_id = sites.id
join beds on rooms.id = beds.room_id group by beds.room_id
order by
    sites.denomination,
    rooms.floor

它的工作非常好,但我想在网站上添加一个选项(field sites.id)。我总是得到错误:&#34;你的SQL语法有错误;检查手册...&#34;

谢谢!

4 个答案:

答案 0 :(得分:1)

SELECT
sites.denomination,
rooms.floor,
rooms.number,
(COUNT(beds.id) - SUM(if(beds.state<0,1,0))) AS active
FROM rooms
JOIN sites ON rooms.site_id = sites.id
JOIN beds ON rooms.id = beds.room_id 
//your WHERE should be here
GROUP BY beds.room_id
ORDER BY
    sites.denomination,
    rooms.floor

答案 1 :(得分:0)

select 
sites.denomination,
rooms.floor,
rooms.number,
(count(beds.id) - sum(if(beds.state<0,1,0))) as active
from rooms
join sites on rooms.site_id = sites.id
join beds on rooms.id = beds.room_id 
where sites.id = ...
group by beds.room_id
order by
    sites.denomination,
    rooms.floor

答案 2 :(得分:0)

就像这样“

select...
from...joins
where....
group by..
having.....
order by...
limit....

有关详细信息,请参阅MYSQL Docs

所以在你的情况下就像是:

select 
sites.denomination,
rooms.floor,
rooms.number,
(count(beds.id) - sum(if(beds.state<0,1,0))) as active
from rooms
join sites on rooms.site_id = sites.id
join beds on rooms.id = beds.room_id 
where ...........//Add your where condition here
group by beds.room_id
order by
    sites.denomination,
    rooms.floor

答案 3 :(得分:0)

试试这个:

select 
sites.denomination,
rooms.floor,
rooms.number,
(count(beds.id) - sum(if(beds.state<0,1,0))) as active
from rooms
inner join sites on rooms.site_id = sites.id
inner join beds on rooms.id = beds.room_id 
where **instruction**
group by beds.room_id
order by
sites.denomination,
rooms.floor