MySQL SELECT when not null - 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;

Related

How to put where clauses in an inner join clause?

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'

inner join two tables having null values on the foreign keys of each other

I have the following data in the database in table1
I run the following query and returns nothing
SELECT
someColumns
FROM table1
INNER JOIN table2 ON table1.id = table2.id
INNER JOIN table3 ON table1.anotherId = table3.anotherId
I want to return all the records in (table2 with id = table1.id and table3 with anotherId = table1.anotherId)
You should using LEFT OUTER JOIN to get the specified result.

MySQL: Expressing full joins with two tables having foreign keys to a common base table

Suppose we have the need for a query performing two FULL JOINs (in PostgreSQL terminology) over table1-3, where table2 and table3 both have foreign keys pointing into table1. This shall work in MySQL, where FULL JOIN is not available. Thus a workaround has to be found.
Starting with LEFT JOINs, the following code works as described in (1):
SELECT table1.ID, table1.x, table2.y, table3.z
FROM (table1
LEFT JOIN table2
ON table1.ID = table2.FOREIGN_ID)
LEFT JOIN table3
ON table1.ID = table3.FOREIGN_ID
Following (2), I'm able to emulate a full outer join using the UNION statement for a single join:
SELECT table1.ID, table1.x, table2.y
FROM table1
LEFT JOIN table2
ON table1.ID = table2.FOREIGN_ID
UNION
SELECT table1.ID, table1.x, table2.y
FROM table1
RIGHT JOIN table2
ON table1.ID = table2.FOREIGN_ID
yielding
and
SELECT table1.ID, table1.x, table3.z
FROM table1
LEFT JOIN table3
ON table1.ID = table3.FOREIGN_ID
UNION
SELECT table1.ID, table1.x, table3.z
FROM table1
RIGHT JOIN table3
ON table1.ID = table3.FOREIGN_ID
ORDER BY ID
yielding
I need advice about how to get to this output from here:
That is, the result rows, which reference an entry from table1 via foreign key should be joined soundly, while the stray entries from table2 and table3 (those with foreign key NULL) should be listed in extra rows (y2 and z4 in the example).
Turned out that this would be a trivial two-step FULL JOIN with in PostgreSQL:
SELECT table1.id, table1.x, table2.y, table3.z
FROM table1
FULL JOIN table2
ON table2.Foreign_Id = table1.id
FULL JOIN table3
ON table3.Foreign_Id = table1.id
ORDER BY table1.id, table2.id, table3.id;
Shoutouts to the guys from freenode #sql pointing out this equivalent MySQL query, which avoids using FULL JOIN:
SELECT id1, x, y, z FROM (
SELECT table1.id as id1, table1.x, table2.id as id2, table2.y, table3.id as id3, table3.z
FROM table1
LEFT JOIN table2
ON table2.Foreign_Id = table1.id
LEFT JOIN table3
ON table3.Foreign_Id = table1.id
UNION ALL
SELECT NULL, NULL, table2.id, table2.y, NULL, NULL
FROM table2 WHERE NOT EXISTS (SELECT 1 FROM table1 WHERE table1.id = table2.Foreign_Id)
UNION ALL
SELECT NULL, NULL, NULL, NULL, table3.id, table3.z
FROM table3 WHERE NOT EXISTS (SELECT 1 FROM table1 WHERE table1.id = table3.Foreign_Id)
ORDER BY id1, id2, id3 ) AS T

MySQL JOIN - Don't want NULL Value

Lets say I have 2 tables:
table1
table1_id
table1_name
table2
table2_id
table2_name
table2_description
table1_id
I join like so:
SELECT * FROM table1 LEFT JOIN table2 ON table1.table1_id = table2.table1_id
How can I have table1_id return its default value instead of NULL when there are no matches in table2?
Answer: Since table1_id exists in both tables, I needed to used aliases.
SELECT *, table1.table1_id AS tid, table2.table1_id AS t2id FROM table1 LEFT JOIN table2 ON table1.table1_id = table2.table1_id
If I understand correctly, you bring in the default row and then use logic in the select:
SELECT t1.*,
COALESCE(t2.table2_name, t2def.table2_name) as table2_name,
. . .
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.table1_id = t2.table1_id LEFT JOIN
table2 t2def
ON t2def.table1_id = $defaultid;
SELECT table1.*
table2.table2_id,
table2.table2_name,
table2.table2_description,
ifnull(table2.table1_id, table1.table1_id) t2id
FROM table1 LEFT JOIN table2
ON table1.table1_id = table2.table1_id
Again, there is no point to check if table2.table1_id is null, as it is the join column ... always either NULL or exactly same as table1.table1_id

MYSQL: How to make it virtual column in select Query?

Below is the my question any one have idea how to resolve this issue:
Example Query:
SELECT id, table2.id as global_id, table3.id as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc
Basically i don't want to write two times "global_id" column in the select statement and also in the some rows have id = NULL
Can you please write working query here. How can i get id value of all rows in the global_id column ?
Thanks
Use COALESCE:
SELECT table1.id, COALESCE(table2.id, table3.id) as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc
Guess you need this:
SELECT id, table2.id as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
WHERE etc etc
UNION ALL
SELECT id, table3.id as global_id
FROM table1
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc