Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Star (*) with mysql inner join?

I have always done inner joins that look like this

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Where you specify each row you want. I am currently working with a table that has 50 rows however and I don't want to type all these SQL joins with all the rows, is there a way to say "select * from Orders, then give me Customers.CustomerName join on ...." instead of specifying every row from the first table?

like image 221
Adam James Avatar asked Dec 20 '22 01:12

Adam James


2 Answers

SELECT Orders.*, Customers.CustomerName

You simply define what you want via tablename.column so you can either specify an existing column name, or use star * to indicate all columns from that table.

like image 180
Lee Avatar answered Dec 29 '22 00:12

Lee


In applications it's highly recommended to be explicit about columns you want to select and to avoid *, but if you are running these queries for yourself it makes sense to use it:

SELECT table_name.*

Note that you can also omit the qualifying table name from the column if the column name is unique. That is Customers.CustomerName can just be CustomerName unless Orders also has a CustomerName column. In the same vein, Orders.OrderID can just be OrderID.

like image 28
Explosion Pills Avatar answered Dec 29 '22 00:12

Explosion Pills