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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址