简体   繁体   中英

mysql find all the possible combinations of one column

I have the table UserContacts with a column like

contact_id

  • 1
  • 5
  • 6

and I want to get the following result (which is all the possible pair combinations of the values of that column)... (1,5),(1,6),(5,6)

is it possible?

This should work

SELECT t2.contact_id,t1.contact_id FROM UserContacts t1 JOIN UserContacts t2 
WHERE t1.contact_id <> t2.contact_id

Sure, make a self-join:

SELECT a.contact_id a, b.contact_id b
FROM   UserContacts a
  JOIN UserContacts b ON b.contact_id > a.contact_id

See it on sqlfiddle .

USE CROSS JOIN

SELECT a.contact_id FROM UserContacts a CROSS JOIN UserContacts b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM