Mysql `using` syntax for multiple tables and one joining column - mysql

Instead of
SELECT * FROM a, b, c
WHERE a.jid = b.jid AND b.jid = c.jid
or
SELECT * FROM a JOIN b USING(jid) JOIN c USING(jid)
I'd like to use something like:
SELECT * FROM a, b, c USING(jid)
This may not seem like much of a convenience but when my query runs accross 4 tables and perhaps more, this could be a real helper.
I.e. there's one column that unites quite a few tables in my database. Is there a way to query multiple tables by naming that column only once?

You can use NATURAL JOIN to automatically join the tables based on columns with the same name. However, you will need to make sure that jid will be the ONLY column with the same name, otherwise you will not get the results that you want.
A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables.
SELECT * FROM a
NATURAL JOIN b
NATURAL JOIN c;

Related

Perform an INNER JOIN with more than 2 tables in MySQL

I just recently learned about SQL INNER JOIN and I thought of applying it on a project so basically I have three tables
payers
discounts
items
Now I was just wondering if I can return the results from both of the three tables at once using an INNER JOIN with both of the 3 tables or is it only possible with 2 tables?
If it is possible to use an INNER JOIN with more than 2 tables then kindly please guide me on how to do it and if not then tell me how to do it in any other ways possible.
Now this is the query that I currently have which doesn't work as expected:
SELECT *
FROM payers
INNER JOIN discounts AND items
ON payers.id = discounts.id AND ON payers.id = items.id;
You want two joins. The syntax is:
SELECT *
FROM payers p
INNER JOIN discounts d ON d.id = p.id
INNER JOIN items i ON i.id = p.id
Side notes:
you did not show your actual schema, so this uses the join conditions described in your attempt; you might need to review that
table aliases make the query shorter to write and easier to read
SELECT * is generally not good practice; instead, I would recommend enumerating the columns you want in the SELECT clause, and properly aliasing conflicting columns names, if any (here, all three tables have a column called id, which would cause ambiguity in the resultset)

JOIN or Union of two tables - SQL

How can I unite two select statement in one table result?
For instance in the first table I want to get everything however on my 2nd table I only want the corel name that is equal to the corel_id and id of my 2nd table?
SELECT *
FROM garage
UNION
SELECT c.name
FROM corel as c
WHERE EXISTS (SELECT 1 FROM garage as g WHERE c.id = g.corel_id
I tried to execute this but this did not work. Is this right? or is there a better way to do this?
Sorry newbie here.
UPDATE EXPECTED RESULT :
https://anotepad.com/notes/b6662w
Give this a try:
SELECT g.*, c.name
FROM garage g
LEFT JOIN corel c
ON c.id = g.corel_id
Matching two tables in a database is called a join. An inner join, the default, returns only the rows that match from both tables.
A left join returns all the rows from the first table whether or not they match the second, and any data from the second table that matches. The right join does the inverse, returning only non-matching data from the second table. There is also the full join that returns all data regardless of match.
A join statement is what you need. A join puts columns from multiple tables into rows together based in the matching conditions in the where clause.
A union requires 2 or more queries to have the same columns. The union puts the sets of rows together into a longer set or rows.

How to select from multiple tables when some of the tables are empty in MySQL

I need to fetch data from 5 tables(all columns of each table) all have FK, which is PK of single table.
But some of the tables may have record may be empty.If data is present on the respective column/table it should return otherwise null/default value
There is one to many and one to one relations on the child tables with parent table.
I have tried so far
- UNION which has concern of same number of columns
- CROSS JOIN not returning any data
- SELECT ALL_COLUMN FROM ALL_TABLE WHERE TABLE.FK=ID Not returning any data
- LEFT JOIN working for 2 tables but not more than that
SELECT A.GENDER, B.BLOCKED_USER FROM t_macroworld_registration AS A
LEFT JOIN t_macroworld_blacklist AS B ON 1=1 WHERE A.ID=15
What are the possible ways I can implement this in a view in MySQL.
Outer join operations are the normative pattern...
SELECT ...
FROM a
LEFT JOIN b ON b.a_id = a.id
LEFT JOIN c ON c.a_id = a.id
LEFT JOIN d ON d.a_id = a.id
WHERE a.id = 15
It's important for the predicates on the outer joined tables to be in the ON clause and not the WHERE clause. If there's any predicate in the WHERE clause requires that a value from one of the outer joined tables be non-NULL, that will negate the "outerness" of the join, making it into an inner join.
The "big rock" problem with this the result when there are more than one matching rows in b, c and d. If there's five rows from b that match, and three rows from c that match, and two rows from b that match, it's going to look like a lot of duplicates. (5x3x2 = 30 rows to be returned, with a lot of duplicated data on those rows.)
Finally I have solved It,
I broke the whole thing into many select query based on FK from each table, so number of additional row returns and mapping has become easy.
Who ever is getting this kind of problem, if it is possible then break it into many select query instead of one.
SELECT
w.id,w.name,a.address,i.name,i.quantity
FROM
warehouse w
LEFT JOIN address AS a ON a.warehouse_id = w.id
LEFT JOIN item AS i ON i.warehouse_id = w.id
LEFT JOIN order AS o ON o.item_id = i.id
WHERE
w.id = 1
GROUP BY 1,2,3,4;
Will give you an overview of your stock and orders for your warehouses. This will also duplicate some results.
Assuming 2 warehouses, 1 address for each, 3 items per warehouse, 2 orders by item = 2 * 3 * 2 = 12 lines
I recommend adding your LEFT JOIN stage by stage and visualizing the result for each stage. You'll quickly understand why lines are multiplying.
Note the usage of foreign keys and ids in the tables to link the tables.
Good luck

SQL Join: Are selects between more than 2 tables still joins?

When I read about inner or outer joins in SQL, all examples and descriptions are about 2 tables being joined. What if there are more than 2 tables in the query? Is that still considered a join?
I think inner join still makes sense even if it is between multiple tables; but I'm not sure outer joins makes sense between more than 2 table.
Can someone please clarify this issue?
Inner joins and outer joins are perfectly reasonable to use with more than 2 tables.
Inner joins force the result to display only data that has whatever row you joined on, whereas outer joins display all data no matter what.
Let us say you wanted to join 4 tables together...
select * from testtable
inner join testable2 on col1 = othercolumn
inner join testable3 on col2 = othercolumn
leftjoin testable4 on col3 = othercolumn
In this case, it would return only results that existed in the inner joins, but the result would not have to exist in the outside/left join. You are forcing testtables 2 & 3 to have a value on what you are joining on.. it cannot be null.
The left join does not care if the value is null, and will show results anyway.
I hope this helps some... Basically.. if you inner join on a value, and it can possibly be null, then the entire query will show blank. This is the scenario you would use an outter join.. you are not forcing the value to exist.
Most examples of joins will include two tables. However, joins can be done on any number of tables.
You can read more about joins all over the interwebs, but you might want to start with:
http://www.w3schools.com/sql/sql_join.asp
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
The w3schools article first thing stated is:
SQL joins are used to combine rows from two or more tables.
This isn't entirely true, as you can even join tables on themselvees!
Consider:
Employees
-----
EmployeeId
ManagerId
EmployeeName
if you want to find out the employees of a specific manager, that could be written as:
select manager.EmployeeName, subordinates.*
from employees manager
inner join employees subordinates on manager.employeeId = subordinates.managerId
For multiple table joins consider:
Employees
----
EmployeeId
ManagerId
EmployeeName
Departments
----
DepartmentId
DepartmentName
EmployeeDepartments
----
DepartmentId
EmployeeId
In this case, if you wanted to find out all department names that employee 5 belonged too, you could do:
select d.DepartmentName
from employees e
inner join employeeDepartments ed on e.employeeId = ed.employeeId
inner join departments d on ed.departmentId = d.departmentId
where e.employeeId = 5
TLDR; - yes including more than 2 tables is still considered join(s)
Every join clause is (logically) between two virtual tables but the virtual tables themselves can be defined as joins on further tables.
So in the following example
SELECT Foo
FROM A
INNER JOIN B
ON A.X = B.X
INNER JOIN C
ON C.Y = A.Y
AND C.Z = B.Z
it can be considered that logically A joins to B then the virtual table (A x B) is joined to C. Columns from all three of those tables are thus available in the final ON clause.
You can control the virtual tables that are evaluated by the placement of the ON clause.
The following example creates a virtual table (A x B) and a virtual table (C x D) and then joins these two together.
SELECT Foo
FROM A
INNER JOIN B
ON A.X = B.X
INNER JOIN C
INNER JOIN D
ON C.Y = D.Y /*Only C and D in scope here*/
ON A.Z = D.Z /*All tables back in scope*/
The query optimiser is free to actually implement the joins in any way that maintains the semantics however. As inner joins are commutative and associative the tables in the above example can be freely re-arranged. For outer joins re-arranging them could change the semantics.

SQL joining multiple tables on single column

Can I use the USING clause to join more than 2 tables? i.e. can I do this
SELECT * FROM (a, b, c) USING(date)
or something similar instead of
SELECT * FROM a, b USING(date), c USING(date)
?
I am using MySQL.
EDIT
I see from the answers that I am misunderstood. The second example works fine, but just gets very long and verbose for many tables. I am looking for a cleaner syntax, not even more complicated ways to achieve this.
There is no such syntax to achieve what you want, however there is one thing that comes close:
It's kind of looked down upon, but you could look into using is the NATURAL JOIN syntax, where the condition(s) used for the join are implicit in that you don't need to specify the columns to join on. The implicit condition happens on columns that have the same name across the tables you're joining.
If date is the only column that has the same name across all of your tables, then you could do something like this:
SELECT *
FROM a
NATURAL JOIN b
NATURAL JOIN c
This will implicitly join the tables on the column date because the field name is the same in the tables.
The downside of this approach is of course the fact that you can't have any other columns with the same name in another table or else your joins will break as those columns will also be factored into the join condition unintentionally.
More on NATURAL JOIN
No, unfortunately you can't use the syntax in your example. It's not valid SQL.
Instead you could use any of the following:
You can use the explicit JOIN syntax to achieve what you want.
SELECT *
FROM x
LEFT JOIN y USING(date)
LEFT JOIN z USING(date);
Usually for better control you'd use the ON clause.
SELECT *
FROM x
LEFT JOIN y ON x.date = y.date
LEFT JOIN z ON y.date = z.date;
Use UNION ALL
SELECT * FROM a WHERE myDate = 'yourDate'
UNION ALL
SELECT * FROM b WHERE myDate = 'yourDate'
UNION ALL
SELECT * FROM c WHERE myDate = 'yourDate'
You can use JOIN clause to join two tables, but the two tables must be connected with foreign key, for example:
SELECT Persons.Name
FROM Persons
INNER JOIN Orders
ON Persons.Id = Orders.Id;