【问题标题】:sql server 2008 select same field with different values [closed]sql server 2008选择具有不同值的相同字段[关闭]
【发布时间】:2013-12-01 11:14:13
【问题描述】:

我需要你的支持

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

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

我想在Description=DesignDescription=Log 的位置显示piddescription 字段

【问题讨论】:

标签: sql sql-server-2008 select


【解决方案1】:

试试这个:

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

或者类似的东西..

【讨论】:

  • 您确实将“YourTable”部分更改为您的表名......对吗?如果您没有符合您的条件的数据。这个查询很好。
  • 现在应该可以工作了,我已经编辑了,因为我能够看到表格结构。
  • 不,请检查我的Q?再次,它说当 description='Design' 和 Description='Log' 比显示记录时
  • 完成。我的错,没有注意到我的错误
  • 我使用了以下 sql query = select pid,Description from ytest as t where t.Description='Design' and t.Description='Log' = no record display
【解决方案2】:

使用DesignLog 选择记录

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

【讨论】:

  • 我使用了以下 sql query = select pid,Description from ytest as t where t.Description='Design' and t.Description='Log' = no record display
  • 你可以在 sqlfiddle.com 中发布查询
  • 或者告诉上面应该是什么 o/p
  • 告诉我们你的桌子叫什么名字?
  • 表名是table!!
【解决方案3】:
SELECT table.PID, table.Description 
FROM table 
WHERE
table.Description='Design'
AND
table.Description='Log'

【讨论】:

    【解决方案4】:

    如果您想在表的某行中选择所有具有“设计”和“日志”描述的所有此类 PID,例如 table1,请尝试:

    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 ;
    

    【讨论】:

    • 谢谢您的及时回复,我有 10 种设计,所以怎么可能在那里用例
    • 让我重新表述一下..您想选择所有这样的 pid,其中至少存在一行,其 [Description] 为 design1 或 design2 或 .. design10 并且还存在一个这样的行[描述] 作为“日志”?
    • 当 description='Design' and Description='Log' and Description='' and .....
    • 您想要所有这样的 pid,其中 10 种设计中的每一种都有一行,并且至少有一行用于描述“日志”?
    • 不,亲爱的,我想要那些记录 pid, descprition where descrption='Any data' and descrption='Any data' and descrption='Any data' ......
    【解决方案5】:

    也许这对你有用

    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%'
    

    【讨论】:

    • 非常感谢,已经解决了!!!!!!
    • 再次显示另一个问题,实际上我需要检查所有值 y1.description='Design' 和 y1.Description='Log',不想使用 y2.description。请尽快回复。谢谢
    • 只需将所选列减少到y1.pid,然后将其作为子查询放入`SELECT * FROM ytest WHERE pid IN ()
    • 或第三次加入 ytest...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多