简体   繁体   中英

MySQL - How to SELECT multiple columns in one query?

I'm creating a database for a bingo card program. I have 2 tables. One is the 'card_cd':

card_cd:

NAME  | TYPE
id_cd | int
col1_id_cd | int
col2_id_cd | int
col3_id_cd | int
col4_id_cd | int
col5_id_cd | int

The other one is 'card_column_cl':

card_column_cl

NAME  | TYPE
id_cl | int
order_cl | int
n1_cl | int
n2_cl | int
n3_cl | int
n4_cl | int

The bingo cards is composed of 5 columns with 4 numbers each (total of 20 numbers), with a universe of 40. Only numbers 1-8 should be in the first column, 9-16 to the second, and so on. What I did is that I generate all possible combination of columns; so if from 8 numbers taken 4, there are 70 possible combination for each column. We have 5 columns so there are 350 different columns. Those columns are saved in the 'card_column_cl' table. In the 'card_cd', I only saved the column ids (id_cl) for each column (to avoid redundancy and too much data). My problem now is how to query a SELECT statement that would contain all numbers in the column, like this:

NAME   |   VALUE
id_cd    | 123456
col1.n1_cl  |  1
col1.n2_cl  |  2
col1.n3_cl  |  3
col1.n4_cl  |  4

col2.n1_cl  |  9
col2.n2_cl  |  10
col2.n3_cl  |  11
col2.n4_cl  |  12

col3.n1_cl  |  17
col3.n2_cl  |  18
col3.n3_cl  |  19
col3.n4_cl  |  20

col4.n1_cl  |  25
col4.n2_cl  |  26
col4.n3_cl  |  27
col4.n4_cl  |  28

col5.n1_cl  |  33
col5.n2_cl  |  34
col5.n3_cl  |  35
col5.n4_cl  |  36

I don't know how to write the query string. Please help me. Thanks a lot :D

The only way you can get that result is with something like this:

SELECT *
FROM card_cd
INNER JOIN card_column_cl AS col1 ON (col1.id_cl = card_cd.col1_id_cd)
INNER JOIN card_column_cl AS col2 ON (col2.id_cl = card_cd.col2_id_cd)
INNER JOIN card_column_cl AS col3 ON (col3.id_cl = card_cd.col3_id_cd)
INNER JOIN card_column_cl AS col4 ON (col4.id_cl = card_cd.col4_id_cd)
INNER JOIN card_column_cl AS col5 ON (col5.id_cl = card_cd.col5_id_cd)

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