-1

我有一个查询:

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 运算符 in order 子句的其他文档来解决我的问题,需要使用的不是 p.date,而是仅使用日期,并且在第二个语句中需要选择在第一个语句中选择的列数

4

4 回答 4

1

也许你想要的是UNION ALL

于 2012-09-06T07:44:49.317 回答
0

尝试这个:

WHERE  p.id_peoples <> ".$this->id." 
       OR ( o.id_contacts = p.id AND
            o.id_status = k.id AND
            ...
          )
于 2012-09-06T07:48:28.780 回答
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 ");
于 2012-09-06T08:53:24.010 回答
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.

于 2012-09-06T07:43:53.780 回答