sql server 2008选择具有不同值的相同字段

时间:2013-11-18 10:29:14

标签: sql sql-server-2008 select

我需要你的支持

我有一个表格,数据显示如下:

Id     :   Descrption  : PID
-----------------------------
1         Design         1
2         Log            1
3         Modern         2
4         Log            2
5         Design         2
6         Log            3

我想在piddescription

中显示Description=DesignDescription=Log字段

5 个答案:

答案 0 :(得分:0)

试试这个:

SELECT T.PID, T.Description 
FROM ytest T 
WHERE
T.Description='Log'
OR
T.Description='Design'

或者沿着这些方向发展......

答案 1 :(得分:0)

选择DesignLog

的记录
SELECT pid,
       Description 
FROM ytest
WHERE Description='Design' 
      OR Description='Log'

答案 2 :(得分:0)

SELECT table.PID, table.Description 
FROM table 
WHERE
table.Description='Design'
AND
table.Description='Log'

答案 3 :(得分:0)

如果你想选择所有这样的PID,它们在表格的某一行中有“设计”和“记录”,那么请尝试:

select t1.pid ,t1.[Description]
from table1 t1
where t1.[Description] in ( 'Design' ,'Log')
and 1= case when t1.[Description] = 'Design' 
            and  exists (select t2.pid 
                        from table1 t2
                        where t2.[Description] = 'Log' 
                        and t1.PID = t2.PID)
            then 1
            when t1.[Description] = 'Log' 
            and  exists (select t2.pid 
                        from table1 t2
                        where t2.[Description] = 'Design' 
                        and t1.PID = t2.PID)
            then 1
      else 0 end ;

答案 4 :(得分:0)

也许这对你有用

SELECT
    y1.pid
    ,y1.description
    ,y2.description
FROM
    ytest AS y1
INNER JOIN
    ytest AS y2
ON
    y1.pid = y2.pid
AND y1.description = 'Log'
AND y2.description like 'Design%'