我有一个问题:
select
p.id AS id,
p.text AS text,
p.date AS date,
k.color AS color,
...
FROM
cform_mba_contacts p,
cform_mba_contacts_category_links o,
cform_mba_contacts_status k,
...
where
p.id_peoples = ".$this->id." AND
o.id_contacts = p.id AND
o.id_status = k.id AND
...
order by p.date
此查询在引入新条件之前完美运行。我需要选择具有和条件且仅使用p.id_peoples = ".$this->id."
的数据。我试图使用UNION
,但这不起作用。我怎么能这样做?
更新
我通过阅读有关UNION运算符的其他文档来解决我的问题 在order子句中需要使用not p.date而不是date,而在second语句中需要选择在第一个语句中选择的列数
答案 0 :(得分:1)
也许你想要的是 UNION ALL 。
答案 1 :(得分:0)
我认为你正在寻找这个:
WHERE 1 = 1
AND (@id IS NULL
OR
( p.id_peoples = @id
AND o.id_contacts = p.id
AND o.id_status = k.id
))
ORDER BY p.date
@id
是.$this->id.
传递的参数。
仅当o.id_contacts = p.id AND o.id_status = k.id
传递的参数不为空时,才会应用条件.$this->id.
。
答案 2 :(得分:0)
试试这个:
WHERE p.id_peoples <> ".$this->id."
OR ( o.id_contacts = p.id AND
o.id_status = k.id AND
...
)
答案 3 :(得分:0)
create a string and execute the string at the last.
decalre @thequery varchar(max);
set @thequery ='select * from emp'
if (1=1)
begin
set @thequery = @thequery + ' where emp_id =1'
end
else
begin
set @thequery = @thequery + ' where emp_id=0'
end exec("@thequery ");