当列的总和等于某个值时,如何选择表的随机行? (MySQL的)
Value : 3
Name Price
------------------------
A 1
B 2
C 1
D 3
E 2
我想得到所有可能性(A + B,D,E + C ......)。
我试过这样做:
SELECT * FROM table HAVING SUM(column) = 3 ORDER BY RAND();
感谢' S Thibeault
答案 0 :(得分:1)
您需要将表连接到自身以获取行对。
SELECT t1.name as name1, t1.price as price1, t2.name AS name2, t2.price AS price2
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.price + t2.price = 3
ORDER BY RAND()
LIMIT 1