MySQL如何查询两列互不重复的记录?
PHP中文网
PHP中文网 2017-04-17 11:08:11
0
2
318

数据如下:

id     date         fromId      toId
--------------------------------------
 1     2013-01-01     1           2
 2     2013-01-02     2           1
 3     2013-01-03     1           3
 4     2013-01-04     3           1
 5     2013-01-05     4           1
 6     2013-01-06     1           4

如何才能查询出fromId或toId包含某个值,但fromId和toId不相互重复的数据? 例如,查询fromId或toId包含1,去除fromId和toId中数据互换的列,仅取日期最大的值,查询结果为:

 id     date         fromId      toId
--------------------------------------
 2     2013-01-02     2           1
 4     2013-01-04     3           1
 6     2013-01-06     1           4
PHP中文网
PHP中文网

认证0级讲师

Antworte allen(2)
洪涛

SELECT max(`date`),maxId,minId FROM (SELECT `date`,IF(fromId>toId,fromId,toId) AS maxId,IF(fromId>toId,toId,fromId) AS minId FROM `table`) AS `tmp` GROUP BY maxId,minId

刘奇

select distinct fromId,* from table group by toId;

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!