2015-06-01 125 views
0

我這裏有樣本條目的數據庫表包括:SQL:創建一個搜索查詢的多個列

enter image description here

我想要做的就是創建一個查詢,將搜索CLIENT_NAME使用LIKE,並且該client_name應該有一個類型,一個狀態和一個信用額度,以及一個barangay或一個城市。

我試圖混合和匹配我的查詢,但我似乎無法得到正確的。

例如,我嘗試這樣做:

select * 
from client 
where client_name like '%%' 
having brgy = '' or city = '' 
and type = '' and status = '' 
and credit_limit = '' 
order by client_name asc; 

PS:我也想這樣做,如果我搜索「安妮」,用「安妮」所有客戶機名稱上會出現

+0

哪些DBMS您使用的是什麼樣的? Postgres的?甲骨文? –

+0

@a_horse_with_no_name我正在使用MySQL – achll

+0

Whoa whoa whoa,'HAVING'用於具有「GROUP BY」子句的查詢。 –

回答

0

需要括號。你做不是需要having條款。你需要了解平等之間的差異,不等於:

select * 
from client 
where client_name like '%%' and 
     (brgy <> '' or city <> '') and 
     type <> '' and status <> '' and 
     credit_limit <> '' 
order by client_name asc; 

注:如果值實際上NULL,而不是空白,那麼這個版本將無法正常工作。然後您需要is not null而不是<> ''

+0

如果我有值爲null?我該怎麼辦? – achll

+0

字段爲空時不起作用。 – achll

+0

對於空值,您使用「is null」或「is not null」。所以,它可能是((brgy <>「」和brgy不爲null)或(city <>「」而city不是null)) – CargoMeister

0

你應該做這種

SELECT * 
FROM client 
WHERE `client_name` LIKE '%%' 
AND `brgy` NOT NULL AND `city` NOT NULL 
AND `type` NOT NULL AND `status` NOT NULL 
AND `credit_limit` NOT NULL 
ORDER BY `client_name` ASC;