Change mysql table join based on field value - mysql

Is it possible in MySQL to change the JOIN of a table based on whats in a particular field of a record?
Example:
SELECT
CASE table0.status
WHEN 1 THEN table1.id
WHEN 2 THEN table2.id
END
FROM
table0, table1, table2
IF (table0.status = 1, INNER JOIN queue ON table1.id = table0.product, INNER JOIN queue ON table2.id = table0.product)
I need the joined table to be table1 if the value for 'product' in table0 = 1 or table2 if the value for 'product' in table0 is 2.
When I try the above example I only get mysql syntax errors. Ive also tried it with CASE statement instead of the IF but still not working.

I think a subselect will be a better solution than JOIN in this case. You can use a CASE statement with a SELECT inside it.

Related

Left join FROM multiple tables

This question is different than the ones I have seen already answered. Is it possible to left join using multiple 'FROM' tables? When I try it (with other code, but the principle is the same) I get the error 'Unknown column 'table1.otherId' in 'on clause'. Any help would be greatly appreciated.
Something like this:
SELECT *
FROM table1, table2
LEFT JOIN other_table
ON other_table.id = table2.id
AND other_table.otherId = table1.otherId
Try like this:-
SELECT *
FROM table1
LEFT JOIN other_table
ON table1.id=other_table.id LEFT JOIN table2 ON
other_table.otherId = table2.id
It depends what you want to do. You seem to want a Cartesian product of the first two tables with lookups on the third:
SELECT *
FROM table1 CROSS JOIN
table2 LEFT JOIN
other_table
ON other_table.id = table2.id AND
other_table.otherId = table1.otherId;
Commas -- which should just be banned permanently from FROM clauses -- are similar to CROSS JOINs. However, the parsing of the SQL statement is different. A comma prevents the tables before it from being used in ON clauses after it; that is the source of your error.
Answer is "No"
But you can do it this way:
SELECT * FROM table2, other_table ON other_table.id = table2.id
LEFT JOIN table1 ON other_table.otherId = table1.otherId

Specify table-column on "in" operator

i would like to know if there is any shortcut to specify the column where IN have to check for matches.
Example:
Instead of this:
select *
from table1
where id in(
select column
from table2
)
something like this:
select *
from table1
where id in table2.column
I know the existence of TABLE for IN, ANY, SOME to specify a table, but it works only if the table specified is composed by just 1 column
EDIT: using join is not an option, because the real use i was looking for is on a NOT IN operator, and also JOIN create duplicates sometimes like in a one to many relation
There is no shortcut like that in SQL. Let me explain why.
In a query, all table references need to be made in the FROM clause. Hence, you cannot simply refer to table2.col unless table2 has been defined in the FROM clause. table2 is actually an alias, which defaults to the table name.
From a performance perspective, I would recommend exists:
select t1.*
from table1 t1
where exists (select column
from table2 t2
where t2.column = t1.id
)
In particular, this can take advantage of an index on table2(column) and has the same semantics as in.
Using a JOIN is a bit shorter. At least it does not require a subquery or another SELECT ... FROM.
SELECT table1.*
FROM table1
JOIN table2 ON table1.id = table2.column
Although this simple example is an inner join, not a semi-join. An inner join is different because it produces one row per matched row in table2. A semi-join only produces one row for each row in table1, even if it matches multiple rows in table2.
If you want to simulate a semi-join, use DISTINCT to reduce the result to one row per row of table1:
SELECT DISTINCT table1.*
FROM table1
JOIN table2 ON table1.id = table2.column
If you want to check for something like NOT EXISTS, use an exclusion join:
SELECT table1.*
FROM table1
LEFT OUTER JOIN table2 ON table1.id = table2.column
WHERE table2.column IS NULL
No need to use DISTINCT on the outer join example. There will be no row duplication from the join, because it can only "match no rows" once.

How to do NOT NULL check in mysql query

Please find below query.
select * from table tab1,
table tab2,
table tab3
where tab1.sid = tab2.sid
and tab2.referencedid = tab3.referencedid
My requirement is tab3's column value which tab3.referencedid is not then use the and condition and populate the records. Please suggest me a way how to achieve this. Based on NOT NULL condition query results should be obtained
As i understand you in this case better to use JOIN instend of using join in Where clause
SELECT *
FROM table1 t1
JOIN table2 t2 ON t1.sid = t2.sid
JOIN table3 t3 ON t2.referencedid = t3.referencedid
WHERE // here you can add your criteria if you have
The syntax for the IS NOT NULL Condition in MySQL is:
field IS NOT NULL

Querying two tables with an union

I have a very simple mysql query, I want to fetch some 'data' from two tables table1 and table2 as soon as this 'data' is in one row containing a precise 'id', so I ran a prepared request :
'select data from (
select data from table1 union select data from table2)
where id = :id'
But it doesn't seem to work (btw, I tried simply 'select data from table1, table2 where id = :id ')
but it didn't work. Someone could help, I don't know where I am missing something ?
select * from table1 where id = :id
union all
select * from table2 where id = :id
Unions are complex and unwarranted in MySQL, I would rather rely on JOIN to execute my statements.
I think what you're looking for is a join. There are multiple types of joins, where the most common are LEFT JOIN and INNER JOIN.
LEFT JOIN returns the rows, even if the matched data does not exists, and return null for the joined data.
INNER JOIN requires the matched data to exists and doesn't return anything if the matched data doesn't exist.
Example using INNER JOIN:
SELECT
table1.*,
table2.*
FROM
table1
INNER JOIN table2 ON ( table1.id = table2.id )
WHERE
id = :id
Select table1.data, table2.data from table1 where table1.id = table2.id

MySql View - Value from one column where some other column is max

I have Table1 and Table2 related on Table1.ID. There can be zero or more Table2 records for a given Table1.ID. I have a view where I want to get Table2.Value where Table2.ID is max for a given Table1.ID. A friend suggested a derived table, but that requires a subquery in the from clause, and MySQL doesn't like that. Are there any other ways to do this? I tried setting up a secondary view to take the place of the subquery, but it seems very slow. I also tried using a having clause to test Table2.ID = MAX(Table2.ID), but it doesn't recognize the column unless I put it into the group by, which screws everything else up.
SELECT t1.*, t2a.*
FROM Table1 t1
LEFT JOIN Table2 t2a
ON (t1.table1_id = t2a.table1_id)
LEFT JOIN Table2 t2b
ON (t1.table1_id = t2b.table1_id AND t2a.table2_id < t2b.table2_id)
WHERE t2b.table2_id IS NULL
AND t1.table1_id = ?;