【问题标题】:Write a query to get output as shown below编写查询以获取输出,如下所示
【发布时间】:2017-01-06 05:28:57
【问题描述】:

一个表名player在表中有一些列和数据如下:

**PID**|**PNAME**|**CITY**|**TEAM**|**SALARY**|**NO_OF_PENALTIES**

  1001   ozil     istanbul  germany  500000      1

  1002   messi     madrid   arsenal  500000      2

  1003  ronaldo    manc      uk      600000      1

  1004  puyol      sussex   germany  400000      3

  1005  fabregas   manchester uk     450000      2

  1006  costa      ankara   turkey   400000      3

  1007  beckham    london   uk       600000      2

这是表格。如果团队名称为“germany”& no_of_penalties=1 则我想记录查询。如果团队名称为“uk”& no_of_penalties=2 则我不想要获取记录。 意思是根据上面的记录按照查询我想得到pid=1001&pid=1003的记录。 但是当我编写如下所示的查询时:

select * from player where (team='germany' and no_of_penalties=1) or not (team='uk' and no_of_penalties=2) and team in ('germany','uk');

然后在执行上述查询后,输出看起来像 pid=1001, 1003, 1004 如下所示

**PID**|**PNAME**|**CITY**|**TEAM**|**SALARY**|**NO_OF_PENALTIES**

1001    ozil     istanbul   germany  500000        1

1003    ronaldo  manc       uk       600000        1

1004    puyol   sussex      germany  400000        3

那么你能帮我解决这个问题吗?

【问题讨论】:

  • 根据您的要求回答正确

标签: sql oracle plsql plsqldeveloper


【解决方案1】:

试试这个

select * from player 
  where (team='germany' and no_of_penalties=1) 
  or (team='uk' and no_of_penalties!=2)

【讨论】:

    【解决方案2】:

    Select * From Player Where (团队 = '德国' AND No_Of_Penalties = 1) 并不是 (团队 = “UK” AND No_Of_Penalties = 2)

    如果我错了,请原谅我,因为我发现您的问题很难清楚地理解,但是您输入了“如果团队名称是“uk”& no_of_penalties=2,那么我不想获得记录”意味着您不想要该值2 但是您选择的答案将显示值为 2 的答案

    【讨论】:

      【解决方案3】:

      试试

          select * from player 
        where (team='germany' and no_of_penalties=1) 
      union all
      select * from player 
        where  (team='uk' and no_of_penalties!=2)
      

      【讨论】:

        【解决方案4】:

        这些是互斥的数据,所以应该使用 AND(而不是 OR)。试试这个

        SELECT *
        FROM player
        WHERE (
                team = 'germany'
                AND no_of_penalties = 1
                )
            AND NOT (
                team = 'uk'
                AND no_of_penalties = 2
                )
            AND team IN (
                'germany'
                ,'uk'
                ) );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-06
          • 2022-08-04
          • 1970-01-01
          • 1970-01-01
          • 2016-06-07
          • 2018-11-01
          • 1970-01-01
          相关资源
          最近更新 更多