现在,我正在使用此查询
SELECT h . * ,
(SELECT state
FROM house_name
WHERE name = h.house_name
AND purpose = h.purpose
AND district_id = (
SELECT id
FROM district
WHERE name = h.district )
LIMIT 1
) AS hstate,
(SELECT name
FROM district
WHERE id = (
SELECT district_id
FROM house_name
WHERE name = h.house_name
LIMIT 1 )
LIMIT 1
) AS origin_d,
(SELECT id
FROM house_name
WHERE name = h.house_name
AND purpose = h.purpose
AND district_id = (
SELECT id
FROM district
WHERE name = h.district )
LIMIT 1
) AS hnameid,
m.display_name
FROM house_detail h
LEFT JOIN members m ON m.id = h.member_id
WHERE
h.deleted =0
AND h.approve =0
AND
(SELECT state
FROM house_name
WHERE state = 'N'
AND name = h.house_name
AND purpose = h.purpose
AND district_id = (
SELECT id
FROM district
WHERE
name = h.district )
LIMIT 1
) IS NOT NULL
AND h.price <=1000000
OR h.price >=70000000
OR (
h.purpose = 'house'
AND h.rent >=100000
)
OR (
h.purpose = 'industry'
AND h.rent >=700000
)
OR h.rent <=5000
在Where子句中:
我想做的是:
h.deleted
,h.approve
和state
必须分别为0
,0
和N
h.rent
和h.price
可能在特定范围内。 目前的问题是它仍然会选择h.deleted
,h.approve
和state
可能是1 or 2
,1
和{{1}的一些数据}}。我认为这是因为他们之间有一些Y
。
有没有办法输出我的预期结果?
答案 0 :(得分:0)
围绕以下陈述设置括号:
WHERE
name = h.district )
LIMIT 1
) IS NOT NULL
AND (h.price <=1000000
OR h.price >=70000000
OR (
h.purpose = 'house'
AND h.rent >=100000
)
OR (
h.purpose = 'industry'
AND h.rent >=700000
)
OR h.rent <=5000)
在最后(
之后打开AND
并在结束时关闭)
...应该这样做