简体   繁体   中英

how to MYSQL compare values from same columns

how do i compare 1 table in mysql? with the following table

name table belajar

   id model name
   1 AAA AAA
   2 BBB BBB
   3 CCC CCC

and I want to generate a query

  id name model idmodel namemodel modelmodel
  1 AAA AAA 2 BBB BBB
  1 AAA AAA 3 CCC CCC
  2 BBB BBB 1 AAA AAA
  2 BBB BBB 3 CCC CCC
  3 CCC CCC 1 AAA AAA
  3 CCC CCC 2 BBB BBB

It sounds like maybe a self join is what you want here:

SELECT
    t1.id,
    t1.name,
    t1.model,
    t2.id AS idmodel,
    t2.name AS namemodel,
    t2.model AS modelmodel
FROM yourTable t1
INNER JOIN yourTable t2
    ON t1.name <> t2.name AND
       t1.model <> t2.model;
ORDER BY
    t1.id,
    t2.id;

The join criteria is basically a cross join, except that you don't want to match two records if the names and models be the same.

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