简体   繁体   English

MySQL-如何在一个查询中选择多个列?

[英]MySQL - How to SELECT multiple columns in one query?

I'm creating a database for a bingo card program. 我正在为宾果卡程序创建数据库。 I have 2 tables. 我有2张桌子。 One is the 'card_cd': 一种是“ 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”:

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. 宾果卡由5列组成,每列有4个数字(总共20个数字),且全域为40。第一列中应仅包含数字1-8,第二列中应包含9-16。 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. 因此,如果从8个数字中选取4个,则每列有70种可能的组合。 We have 5 columns so there are 350 different columns. 我们有5列,所以有350个不同的列。 Those columns are saved in the 'card_column_cl' table. 这些列将保存在“ card_column_cl”表中。 In the 'card_cd', I only saved the column ids (id_cl) for each column (to avoid redundancy and too much data). 在“ card_cd”中,我仅保存了每一列的列ID(id_cl)(以避免冗余和过多的数据)。 My problem now is how to query a SELECT statement that would contain all numbers in the column, like this: 我现在的问题是如何查询将包含列中所有数字的SELECT语句,如下所示:

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 非常感谢: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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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