php - How can I select the lowest number from multiple tables in MySQL? -
lets have 3 tables: table1 id score 1 2 2 5 3 5 table2 id score 1 1 2 2 3 3 table3 id score 1 4 2 4 3 2
how can lowest value each id 3 tables? when execute query post this:
id lowestscore 1 1 2 2 3 2
thanks!!
you can use union
combine records 3 tables , wrap on subquery use min()
lowest score each id
.
select id, min(score) score ( select id, score table1 union select id, score table2 union select id, score table3 ) sub group id
output
╔════╦═══════╗ ║ id ║ score ║ ╠════╬═══════╣ ║ 1 ║ 1 ║ ║ 2 ║ 2 ║ ║ 3 ║ 2 ║ ╚════╩═══════╝
Comments
Post a Comment