简体   繁体   中英

difference between select * and select table name

This is the basic question about sql statements.

What is the difference between

SELECT * FROM "Users"

and

SELECT "Users".* FROM "Users"

[TableName].[column] is usually used to pinpoint the table you wish to use when two tables a present in a join or a complex statement and you want to define which column to use out of the two with the same name.

It's most common use is in a join though, for a basic statement such as the one above there is no difference and the output will be the same.

区别在于其中之一包含两次表名。

In your case there is no difference. It emerges, when you are selecting from multiple tables. * takes data from all the tables, TABLE_NAME.* - all the data from this table. Suppose, we have a database with 2 tables:

mysql> SELECT * FROM report;
+----+------------+
| id | date       |
+----+------------+
|  1 | 2013-05-01 |
|  2 | 2013-06-02 |
+----+------------+

mysql> SELECT * FROM sites_to_report;
+---------+-----------+---------------------+------+
| site_id | report_id | last_run            | rows |
+---------+-----------+---------------------+------+
|       1 |         1 | 2013-05-01 16:20:21 |    1 |
|       1 |         2 | 2013-05-03 16:20:21 |    1 |
|       2 |         2 | 2013-05-03 14:21:47 |    1 |
+---------+-----------+---------------------+------+

mysql> SELECT
    ->  *
    -> FROM
    ->  report
    -> INNER JOIN
    ->  sites_to_report
    -> ON
    ->  sites_to_report.report_id=report.id;
+----+------------+---------+-----------+---------------------+------+
| id | date       | site_id | report_id | last_run            | rows |
+----+------------+---------+-----------+---------------------+------+
|  1 | 2013-05-01 |       1 |         1 | 2013-05-01 16:20:21 |    1 |
|  2 | 2013-06-02 |       1 |         2 | 2013-05-03 16:20:21 |    1 |
|  2 | 2013-06-02 |       2 |         2 | 2013-05-03 14:21:47 |    1 |
+----+------------+---------+-----------+---------------------+------+

mysql> SELECT
    ->  report.*
    -> FROM
    ->  report
    -> INNER JOIN
    ->  sites_to_report
    -> ON
    ->  sites_to_report.report_id=report.id;
+----+------------+
| id | date       |
+----+------------+
|  1 | 2013-05-01 |
|  2 | 2013-06-02 |
|  2 | 2013-06-02 |
+----+------------+

In the case of example given by you, there is no difference between them when it comes to semantics.When it comes to performance it might be too little... just parsing two different length strings....

But, it is only true for the example given by you. Where as in queries where multiple tables are involved tableName.* disambiguate the table from which table we want to select all columns.

Example: If you have two tables TableA and TableB . Let's suppose that they have column with same names that is Name . If you want to specify from which table you want to select Name column. Table-name qualifier helps.

`select TableA.Name, TableB.Name where TableA.age=TableB.age`

That's all I can say.

For the examples you provided, the only difference is in syntax. What both of the queries share is that they are really bad. Select * is evil no matter how you write it and can get you into all kinds of trouble. Get into the habit of listing the columns you want to have included in your result set.

The particular examples specified would return the same result and have the same performance. There would be no difference in that respect, therefore.

However, in some SQL products, difference in interpreting * and alias.* has effect, in particular, on what else you can add to the query. More specifically, in Oracle, you can mix an alias.* with other expressions being returned as columns, ie this

SELECT "Users".*, SomeColumn * 2 AS DoubleValue FROM "Users"

would work . At the same time, * must stand on its own, meaning that the following

SELECT *, SomeColumn * 2 AS DoubleValue FROM "Users"

would be illegal .

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