MySQL 从 SQL 数据库中选择特定的行和列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36682736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select specific rows and columns from an SQL database
提问by dan
Is it possible to retrieve specific columns of specific rows in an SQL query
?
是否可以检索特定行的特定列SQL query
?
Let's say I'm selecting from my SQL
table, called my_table
, rows whose names are: a, b, using this query text:
假设我正在从SQL
名为 的表中选择my_table
名称为:a、b 的行,使用以下查询文本:
"select * from my_table where row_names in ('a', 'b') order by row_names"
How can I modify this query text to select only columns 2,12,22,32,42 rather all its 1000 columns?
如何修改此查询文本以仅选择第 2、12、22、32、42 列而不是所有 1000 列?
回答by Anthony Drogon
Replace the wildcard symbol *
with the column names you want to retrieve.
将通配符替换为*
要检索的列名称。
But please read up the documentation on SQL standard. It is very unlikely you need 1.000 columns in a table.
但请阅读有关 SQL 标准的文档。一个表中不太可能需要 1.000 列。
回答by Ultradiv
Try and read the sql, as it really does what it says
尝试阅读 sql,因为它确实做到了它所说的
select [column name 1, column name 2]
from [table name]
where [column name 3] in ('column value 1', 'column value 2')
order by [column name 3]
please select the values of "column name 1" and "column name 2" from rows in the table called "table name" where those rows have values equal to 'column value 1' and 'column value 2' in the column called "column name 3"
请从名为“表名”的表中的行中选择“列名 1”和“列名 2”的值,其中这些行的值等于名为“列”的列中的“列值 1”和“列值 2”名字 3"
回答by Yatin Goyal
2 imp points
1. 1000 columns in single table is against RDBMS and its highly unnormalised table.
2. rows doesnot have name.
2 imp 点
1. 单个表中的 1000 列针对 RDBMS 及其高度非规范化的表。
2. 行没有名字。
Now if you know names of columns then use following:
现在,如果您知道列的名称,请使用以下内容:
"select column_1,column_2 from my_table where primary_key_column in ('1','2');"
here column_1,column_2 are names of columns you want.
primary_key_column to uniquely define your rows and in brackets all the primary keys you need to retrieve.
这里 column_1,column_2 是你想要的列的名称。
primary_key_column 用于唯一定义您的行,并在括号中包含您需要检索的所有主键。
If you know only know column numbers then use following to retrieve column names:
如果您只知道列号,则使用以下方法检索列名:
"SELECT Column_name FROM user_tab_columns where table_name = 'MY_TABLE' and Column_id in (1,2,5);"
Hope this will help.
希望这会有所帮助。
For further reference http://www.w3schools.com/sql/
回答by MikeC
Yes. SQL is used to select required columns from required rows. The data rows don't have row_names unless you've defined a column with the name row_names (this is not done as a rule). At a basic level, retrieving data from an RDBMS is through an SQL statement that is constructed as follows: (This'd be a SELECT statement. The simplest version has the following clauses)
是的。SQL 用于从所需的行中选择所需的列。数据行没有 row_names ,除非您定义了一个名为 row_names 的列(通常不这样做)。在基本层面上,从 RDBMS 检索数据是通过构造如下的 SQL 语句:(这将是一个 SELECT 语句。最简单的版本具有以下子句)
- Pick the table which holds the data of interest (FROM clause). If we want telephone listings, we might query the whitepages table.
- Identify the rows which hold the data of interest (WHERE clause). This is done by putting conditions on column values (there are no row hearders or row names). We want phone numbers for people whose last name is "Hemingway" Here the last name would be a column in the table, not the row name.
- Pick the data columns we want to retrieve (SELECT clause). We want the first name, last name and phone number.
- 选择包含感兴趣数据的表(FROM 子句)。如果我们想要电话列表,我们可能会查询 whitepages 表。
- 确定保存感兴趣数据的行(WHERE 子句)。这是通过在列值上放置条件来完成的(没有行听者或行名称)。我们想要姓氏为“Hemingway”的人的电话号码 这里姓氏将是表中的一列,而不是行名。
- 选择我们要检索的数据列(SELECT 子句)。我们想要名字、姓氏和电话号码。
Putting it all together, we might have:
把它们放在一起,我们可能有:
SELECT
first_name,
last_name,
phone_number
FROM
WhitePages
WHERE
(last_name = "Hemingway")
In your case, the SELECT statement would be something like:
在您的情况下,SELECT 语句将类似于:
SELECT
<name of column 2>,
<name of column 12>,
<name of column 22>,
<name of column 32>,
<name of column 42>
FROM
my_table
WHERE
(<some_column_name> IN ('a', 'b'))
AND (<some_other_column_name> = <some_number>)
AND (<yet_another_column_name> = "<some_text>")
ORDER BY
<a_selected_column>,
<another_selected_column>
If you're going to work with databases on a regular basis, I'd recommend learning a bit more about SQL as you'll have a difficult time getting by with questions on SO or other web sites.
如果您要定期使用数据库,我建议您多学习一点 SQL,因为您将很难解决 SO 或其他网站上的问题。
While the 1000 columns you mention might be hyperbole, SQL tables do not normally have hundreds of columns. Consider having your tables and SQL designed by an experienced team member.
虽然您提到的 1000 列可能有些夸张,但 SQL 表通常不会有数百列。考虑让经验丰富的团队成员设计您的表和 SQL。