dongrandi8411 2018-04-21 13:31
浏览 189
已采纳

比较同一表中2行之间的所有列

I'm looking to compare all columns between two rows in a table using php/mysqli but just can't figure out how to do it. My table looks something like:

+----------+----------+----------+----------+----------+--------------+
| username | compare1 | compare2 | compare3 | compare4 | compare5 etc |
+----------+----------+----------+----------+----------+--------------+
| Adam     |        1 |        0 |        1 |        1 |            0 |
| Billy    |        1 |        1 |        1 |        1 |            0 |
| Charlie  |        1 |        0 |        0 |        1 |            1 |
+----------+----------+----------+----------+----------+--------------+

I want to select say username Charlie as the child and username Adam as the parent then compare their values for all other columns (there's quite a few) in the table. If any of the child values is 0 where the parent value is 1 then the query returns false, otherwise, it returns true.

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • douluhaikao93943 2018-04-21 13:34
    关注

    You can do:

    select (count(*) = 0) as flag
    from t tp join
         t tc
         on tp.username = 'Adam' and tc.username = 'Charlie'
    where (0, 1) in ( (tp.compare1, tc.compare1), (tp.compare2, tc.compare2),
                      . . .
                    );
    

    Note: This assumes that each username has only one row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率