我希望使用php / mysqli比较表格中两行之间的所有列,但我们无法弄清楚如何做到这一点。我的表看起来像:
+----------+----------+----------+----------+----------+--------------+
| 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 |
+----------+----------+----------+----------+----------+--------------+
我想选择用户名Charlie作为孩子,用户名Adam作为父母,然后比较他们在表格中所有其他列(那里有很多)的值。如果任何子值为0,其中父值为1,则查询返回false,否则返回true。
提前致谢!
答案 0 :(得分:2)
你可以这样做:
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),
. . .
);
注意:这假定每个username
只有一行。
答案 1 :(得分:1)
另一种查询可能是:
select c.compare1 >= p.compare1
and c.compare2 >= p.compare2
and c.compare3 >= p.compare3
and c.compare4 >= p.compare4
and c.compare5 >= p.compare5
as res
from mytable p
, mytable c
where p.username = 'Adam'
and c.username = 'Charlie';