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 ...
Related
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'
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.
Is is possible to simplify this UNION to avoid the near redundancy of the queries being unioned? As seen here, both queries are similar. They just join on a different column in table2. The reason i use Union, instead of just Inner Joining 2x in the same query is because the results must be in 1 column by virtue of the fact that this queries is used as a subquery.
SELECT t1.id as id
FROM table1 g
INNER JOIN table2 t1 on g.t_id = t1.id
WHERE g.id=1
UNION
SELECT t2.id as id2
FROM table1 g
INNER JOIN table2 t2 on g.t2_id = t2.id
WHERE g.id=1
I don't see why this couldn't be treated as a simple inner join that can be satisfied by a match in either of two predicates. I've removed the original table aliases of t1, t2, and g for the sake of clarity. Since I don't know if the query could produce duplicate rows, I used DISTINCT in order to collapse duplicate rows in the same manner that the UNION did in the original query.
SELECT DISTINCT table2.id
FROM table1
INNER JOIN table2
ON ( table1.t_id = table2.id OR table1.t2_id = table2.id )
WHERE table1.id = 1
;
It is possible to do with two joins, and the IFNULL() function:
SELECT IFNULL (t1.id, t2.id) as id
FROM table1 g
INNER JOIN table2 t1 on g.t_id = t1.id
INNER JOIN table2 t2 on g.t2_id = t2.id
WHERE g.id=1
You might find this simpler:
select distinct t.id
from table2 t
where t.id in (select g.t_id from table1 g) or
t.id in (select g.t2_id from table1 g)
However, the performance would be awful on MySQL. You can also do:
select distinct t.id
from table2 t
where exists (select 1 from table1 g where g.t_id = t.id or g.t2_id = t.id)
The second version should work better in MySQL.
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;
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"