我有一個MySQL查詢這是這樣的:MySQL的子查詢的WHERE子句
select
*,
(select meta_value
from nord_usermeta m
where meta_key = 'firm' and user_id = s.user_id
limit 1)
as firm,
(select meta_value f
rom nord_usermeta m
where meta_key = 'first_name' and user_id = s.user_id
limit 1)
as first_name,
(select meta_value
from nord_usermeta m
where meta_key = 'last_name' and user_id = s.user_id
limit 1)
as last_name
from nord_submissions s
order by created_at desc
現在我需要將它縮小與where子句是這樣的:
select
*,
(select meta_value
from nord_usermeta m
where meta_key = 'firm' and user_id = s.user_id
limit 1)
as firm,
(select meta_value
from nord_usermeta m
where meta_key = 'first_name' and user_id = s.user_id
limit 1)
as first_name,
(select meta_value
from nord_usermeta m
where meta_key = 'last_name' and user_id = s.user_id
limit 1)
as last_name
from nord_submissions s
where firm like '%DG%'
order by created_at desc
的問題顯然是在我使用子查詢的方式,但我無法找到一種方法在where子句中引用它。
請幫忙。
「問題」..什麼問題?你可以請擴展你的問題與錯誤消息/示例數據庫模式/示例數據等 - 可能甚至是一個小提琴http://sqlfiddle.com/about.html – Steve 2013-03-15 10:06:35
**企業**是一個用戶定義的列..我們可以稱之爲虛構的列太..和WHERE只適用於數據庫列。你應該用'HAVING'來代替'WHERE' – diEcho 2013-03-15 10:23:53