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"
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'
SELECT t1.*, IFNULL(t2.profile_id, t3.profile_id) AS `profile_id`
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.some_coulmn
LEFT JOIN table3 AS t3
ON t1.id = t3.some_coulmn
LEFT JOIN table4 AS t4
ON profile_id = t4.some_column
I'm trying to use an alias (profile_id) inside my join condition. It fails. Is there a way to do this?
Column aliases defined in th SELECT clause cannot be used in the join conditions. This is because the FROM clause is evaluated before the SELECT clause.
If I followed you correctly, you probably want:
SELECT t1.*, IFNULL(t2.profile_id, t3.profile_id) AS `profile_id`
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.some_coulm
LEFT JOIN table3 AS t3
ON t1.id = t3.some_coulm
LEFT JOIN table4 AS t4
ON IFNULL(t2.profile_id, t3.profile_id) = t4.some_column
I have 2 tables in MySQL
table1
ID|name|group_1|group_2|group_3
Table2
ID|group_name|Group_location
I want to get table 1 ID and show all the data and not tableIDs.
I'm trying to use a join like so
SELECT table1.name, table2.group_1, table1.group_2, table1.group_3
FROM tabl1 JOIN table2 on table1.group_1 = table2.ID AND
table1.group_2 = table2.ID AND table1.group_3 = table2.ID
WHERE table1.ID IN (868)
ORDER BY FIELD(table1.ID,868);
It's only returning the IDs in the result but I want it to return the table2 group_name
Is this what you want?
SELECT t1.name, t21.group_1, t22.group_2, t23.group_3
FROM table1 t1 LEFT JOIN
table2 t21
ON t1.group_1 = t21.ID LEFT JOIN
table2 t21
ON t1.group_2 = t22.ID LEFT JOIN
table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER BY FIELD(t1.ID, 868);
This uses LEFT JOIN in case any of the reference columns are NULL.
Try this;
SELECT t1.name
, t21.group_name
, t22.group_name
, t23.group_name
FROM table1 t1
LEFT
JOIN table2 t21
ON t1.group_1 = t21.ID
LEFT
JOIN table2 t21
ON t1.group_2 = t22.ID
LEFT
JOIN table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER
BY FIELD(t1.ID, 868);
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 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 ...