简体   繁体   中英

MySQL Tricky Pivot Assistance

I am having a bit of a problem with a seemingly simple MySQL query. I have spent some days now trying to figure this out. I get close, but I have not been able to replicate the requested output.

Here are the tables:

create table Schools(ID int, School1 varchar(10), School2 varchar(10));
insert into Schools Values
  (1, "Robertson", "Addison"),
  (2, "Robertson", "Nester"),
  (3, "Robertson", "Franklin"),
  (4, "Addison", "Nester"),
  (5, "Addison", "Franklin"),
  (6, "Nester", "Franklin") ;

  create table Results(ID int, Score int);
  insert into Results values
  (1, 30),
  (2, 20),
  (4, 60),
  (3, 80),
  (1, 40),
  (2, 55),
  (5, 82),
  (4, 65),
  (6, 38),
  (3, 44),
  (5, 25),
  (6, 53);

And here is my required output from the query:

School1|School2|Score1|Score2
Robertson|Addison|30|40
Robertson|Nester|20|55
Robertson|Franklin|80|44
Addison|Nester|60|65
Addison|Franklin|82|25
Nester|Franklin|38|53

Any help would be greatly appreciated!

Also delimiter does not matter, it doesn't have to be pipe.

Is the problem:

  create table Results(ID int, Score int);
  insert into Scores values

Results or Scores??

Easie peasy to do this in PHP do you want the | character as a divider. Bit confused.

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-2025 STACKOOM.COM