How to put where clauses in an inner join clause? - mysql

I am joining a few tables and in each join, I have to specify a specific condition.
SELECT * FROM
table1
INNER JOIN table2
ON table1.id = table2.id WHERE table2.column1 = 'horse'
INNER JOIN table3
ON table2.id = table3.id WHERE table3.column1 = 'cow';
I get a sql syntax error. When I remove the where statements, it starts working again. How am I able to solve this?

Change your wheres on the joins to ands:
SELECT * FROM
table1
INNER JOIN table2
ON table1.id = table2.id AND table2.column1 = 'horse'
INNER JOIN table3
ON table2.id = table3.id AND table3.column1 = 'cow';

There's just one WHERE clause for the entire query, they're not associated with joins. Use AND or OR to combine them.
SELECT *
FROM table1
INNER JOIN table2 ON table1.id = table2.id
INNER JOIN table3 ON table2.id = table3.id
WHERE table3.column1 = 'cow' AND table2.column1 = 'horse'

Related

Does order of inner and outer joins matter?

Are there any circumstances that would cause different number of records returned from a query that includes inner joins and outer joins depending on the order of the joins?
As a simple example, could there be any difference in the resultset generated by this query:
SELECT *
FROM table1
JOIN table2 on table1.id = table2.id
LEFT OUTER join table3 ON table1.id = table3.id
....or by this query which differ only by the 2 join conditions being swapped
SELECT *
FROM table1
LEFT OUTER join table3 ON table1.id = table3.id
JOIN table2 on table1.id = table2.id
For INNER JOIN order doesn't matter. But for OUTER JOIN it does.
'Table1 JOIN Table2' is same as 'Table2 JOIN Table1'
But,
'Table1 LEFT JOIN Table2' is not same as 'Table2 LEFT JOIN Table1'
Outer Joins are neither commutative nor associative. You may checkout EXPLAIN PLAN to understand the performance difference.

MySQL SELECT when not null

I am trying to make a query where 3 tables are being used. The first, table1, is used to store id values for table2 and table3 as well as some other data. In some cases there are no values stored in either of table2 or table3, some cases involve one or the other, and some involve both.
The issue is that when there are not values in either of table2 or table3, SQL attempts to look up NULL values as follows:
SELECT table1.id, table2value, table3value
FROM table1, table2, table3
WHERE table1value1 = table2.id AND table1value2 = table3.id;
So in this query if table1value1 or table1value2 are NULL, the query will not work as it is obviously not a valid link. So I am wondering how I would go about finding only the values that exist.
Use INNER JOIN instead. It joins the tables together with the condition of existing data in all tables. Else that row won't be returned.
SELECT table1.id, table2.value, table3.value
FROM table1
INNER JOIN table2
ON table1.value1 = table2.id
INNER JOIN table3
ON table1.value2 = table3.id;
But if you require for only one of these 2 rows to have existing data, you can do like this:
SELECT table1.id, table2.value, table3.value
FROM table1
LEFT JOIN table2
ON table1.value1 = table2.id
LEFT JOIN table3
ON table1.value2 = table3.id;
WHERE table2.id IS NOT NULL OR table3.id IS NOT NULL
Below query will return only matching rows with value exists in all 3 tables.
SELECT table1.id, table2value, table3value
FROM table1 inner join table2 on table1value1 = table2.id
inner join table3 on table1value2 = table3.id;
I think you need to use INNER JOIN, And by definition, the INNER JOIN keyword return rows when there is at least one match in both tables.
SELECT table1.id, table2.value, table3.value
FROM table1
INNER JOIN table2
ON table1.value1 = table2.id
INNER JOIN table3
ON table1.value2 = table3.id;
try This:
SELECT table1.id,
table2value,
table3value
FROM table1
JOIN table2
ON table1value1 = table2.id
JOIN table3
ON table1value2 = table3.id;
What about INNER JOIN
SELECT table1.id, table2value, table3value
FROM table1 INNER JOIN table2 ON table1.value1 = table2.id,
INNER JOIN table3 ON table1.value2 = table3.id;
You should use INNER JOIN.
SELECT table1.id, table2value, table3value
FROM table1
INNER JOIN table2 ON table1value1 = table2.id
INNER JOIN table3 ON table1value2 = table3.id;

Unknown Column in "On" Clause

I'm working on the following SELECT statement:
SELECT *
FROM table1
LEFT JOIN table2 ON table2.contentid = table3.id AND table2.tmplid = table1.id
WHERE table2.value = "test"
I'm getting the following error:
Execution of a query to the database failed - Unknown column
'table3.id' in 'on clause' ยป
What am I doing wrong?
This is your problem:
LEFT JOIN table2 ON table2.contentid = table3.id
You can't have any table in your ON clause that's not part of the join (in this case, table3).
Did you mean this instead:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.id = table2.tmplid
LEFT JOIN table3
ON table2.contentid = table3.id
WHERE table2.value = "test"
You are referenceing table3.id which is not defined in your sql anywhere. Did you mean to put table1.id?
If you want to reference table3 you will need to join on it:
SELECT *
FROM table1
LEFT JOIN table3
ON table1.id = table3.someid
LEFT JOIN table2
ON table2.contentid = table3.id AND table2.tmplid = table1.id
WHERE table2.value = "test"

Proper order syntax for MySQL INNER JOIN ON (Basic Q)

I have the following MySQL query statement:
SELECT *
FROM table1 INNER JOIN table2
ON table1.id = table2.id
WHERE 1
Does it matter if my inner join on statement is table1.id = table2.id vs. table2.id = table1.id?
There is no functional or performance difference between the two options you presented.
It's purely a stylistic choice.
Personally I prefer this style, but I'm sure there are others who do it differently:
SELECT ...
FROM table1
INNER JOIN table2 ON table2.id = table1.id
INNER JOIN table3 ON table3.id = table1.id
WHERE ...

How to make two JOIN's for same table name in MySql?

SELECT table1.name, table2.wage, table2.bonus table3.shift, table3.endtime, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 ON table2.userid = table3.userid
INNER JOIN table3 ON table2.endtime = table3.endtime
INNER JOIN table4 ON table4.userid = table4.userid
LEFT JOIN table5 ON table1.name = table5.position
this throws error: Not unique table/alias: 'table3'
i tried
INNER JOIN table3 tbl3 ON table2.userid = table3.userid
INNER JOIN tbl3 ON table2.endtime = tbl3.endtime
sill not working
Give it a unique alias:
SELECT table1.name, table2.wage, table2.bonus t3a.shift, t3b.endtime, table4.vacation
FROM table1
INNER JOIN table2 ON table1.userid = table2.userid
INNER JOIN table3 t3a ON table2.userid = t3a.userid
INNER JOIN table3 t3b ON table2.endtime = t3b.endtime
INNER JOIN table4 ON table4.userid = table4.userid
LEFT JOIN table5 ON table1.name = table5.position;
Actually, it makes for neater code to give every table an alias:
SELECT
t1.name,
t2.wage,
t2.bonus,
t3a.shift,
t3b.endtime,
t4.vacation
FROM table1 t1
INNER JOIN table2 t2 ON t1.userid = t2.userid
INNER JOIN table3 t3a ON t2.userid = t3a.userid
INNER JOIN table3 t3b ON t2.endtime = t3b.endtime
INNER JOIN table4 t4 ON t4.userid = t1.userid
LEFT JOIN table5 t5 ON t1.name = t5.position;
It looks like you have an error in your join for table4, and a missing comma in the selects - I have corrected it in this last version.
Use an alias with the keyword AS (which can be omitted).
INNER JOIN table3 AS t31 ON table2.userid = t31.userid
INNER JOIN table3 AS t32 ON table2.endtime = t32.endtime
Try aliasing the table names:
SELECT *
FROM Table1 t1
JOIN Table1 t2
ON t1.X = t2.Y