Select query except another table - mysql

I got 2 tables.
TABLE 1
ID FRANCHISENAME TELEPHONE FRANCHISE_ID
1 BURGER 666-555-999 5
2 JSUBS 666-555-999 7
3 STEAKS 777-888-999 3
TABLE 2
ID NAME TELEPHONE EMAIL FRANCHISE_ ID
5 JOHN 555-444-333 JOHN#GMAIL.COM 5
5 JOHN 555-444-333 JOHN#GMAIL.COM 7
6 EDGARD 555-444-333 EDGARD#GMAIL.COM 9
I want to retrieve all data in table one, except for that data where the user has his email in Table 2. As for example JOHN has franchise_id 5 and 7, so the query would only return
3 STEAKS, 777-888-999, 3

Assuming that TABLE_1 & TABLE_2 relate to each other through TABLE_1.FRANCHISE_ID & TABLE_2.FRANCHISE_ID
You can use NOT EXISTS
SELECT
*
FROM TABLE_1 T1
WHERE NOT EXISTS(
SELECT *
FROM TABLE_2 T2
WHERE T2.FRANCHISE_ID = T1.FRANCHISE_ID
AND T2.EMAIL = 'JOHN#GMAIL.COM'
)
OR
You can use LEFT JOIN along with IS NULL
SELECT
T1.*
FROM TABLE_1 T1
LEFT JOIN TABLE_2 T2 ON T1.FRANCHISE_ID = T2.FRANCHISE_ID
WHERE T2.FRANCHISE_ID IS NULL;

SELECT t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2
ON t1.FRANCHISE_ID = t2.FRANCHISE_ID
AND LEN(IFNULL(t2.EMAIL,'')) > 0
WHERE
t2.ID IS NULL
Even if there is a record in Table 2 if it has no email it will be returned by this query. You can expand say to > 7 or more to check for a minimum level of validity of email address.

This should get you there using the NOT IN function. It will exclude records from Table 1 if there is a matching Franchise ID in Table 2, unless the email field is Table 2 is null:
SELECT * FROM Table1
WHERE Table1.Franchise_ID NOT IN
(SELECT Table2.Franchise_ID FROM Table2
WHERE Table2.Email IS NOT NULL);

Related

Left join with other table with where clause

I have 2 tables in MySQL database that I would like to join, where I would like to contain all results from table1.
My tables looks like this:
table1
id
name
1
name1
2
name2
3
name3
4
name4
5
name5
6
name6
7
name7
8
name8
table2
id
table1_id
myfield
1
3
test1
2
2
test2
3
1
test1
4
4
test2
5
5
null
6
2
null
What I am trying to achieve is to get a table which contains all the rows from table1 and only the data that is joined from table2.
This is my query:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
where myfield="test1"
group by t1.id
But this is not what I want to achieve.
What I want to achieve is to get all records from table1 and to have all related records from table2 where table2.myfield="test1". And for the other for table2.mytable to have null (if they do not fulfil table2.myfield="test1").
Any help is much appreciated!
Thanks!
move the where clause to the on clause:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
and myfield="test1"
group by t1.id
BTW: some DBMS does not allow select * with group by. So select the id and some aggregated values or remove group by id

Can I select rows from two tables using t1 rows as key for t2 rows?

I have two tables:
id1, id2, ... idX contains id from t2 or null. Can I make such select, which will put params from t2 in t1?
For example i have a t1(name,year,id1,id2,id3,id4) values:
name year id1 id2 id3 id4
------------------------------------------
marko 1999 1 2 null 1
polo 1985 null null 5 3
And t2 values:
id info param
--------------------------------------
1 apple green
2 car yellow
3 bee pink
4 doctor whiskey
5 book small
So I'd like to have such dynamical query results, based on t1 rows:
SELECT name, year, id1-info, id1-param, id2-info, id2-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
SELECT name, year, id3-info, id3-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
I googled a lot, but found nothing except nested queries, such as:
SELECT
name,
year,
(SELECT `info` FROM t2 WHERE id = t1.id1) AS id1-info,
(SELECT `param` FROM t2 WHERE id = t1.id1) AS id1-param,
(SELECT `info` FROM t2 WHERE id = t1.id2) AS id2-info...
But I understand, that it is a very bad idea, because I have a lot of id columns in t1, which are not static. Or if it cannot be done dynamically, can I just make SELECT, which will show me all in one row:
SELECT name, year, id1-info, id1-param, id2-info, id2-param, id3-info, id3-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
To use one select in one row, you can use mulitple left join on t2.
SELECT name,
`year`,
id11.info as `id1-info`,
id11.param as `id1-param`,
id21.info as `id2-info`,
id21.param as `id2-param`,
id31.info as `id3-info`,
id31.param as `id3-param`,
id41.info as `id4-info`,
id41.param as `id4-param`
FROM t1
LEFT JOIN t2 as id11 on t1.id1=id11.id
LEFT JOIN t2 as id21 on t1.id2=id21.id
LEFT JOIN t2 as id31 on t1.id3=id31.id
LEFT JOIN t2 as id41 on t1.id4=id41.id
WHERE name = 'marko';
result:
name year id1-info id1-param id2-info id2-param id3-info id3-param id4-info id4-param
marko 1999 apple green car yellow (null) (null) apple green

SQL Combining data from 3 tables to from a 4th table based on some conditions

I have 3 tables:-
table1 :-
ReportType | ResourceId
t2 123
t3 5
table2:-
Id | Name | Created
1 A 10
2 B 11
123 C 12
table3:-
Id | Name | Created
4 D 13
5 E 14
6 F 15
table1's ResourceId and table2 and 3's Id column have same values
I want to create a 4th table like this:-
ReportType | ResourceId | Name | Created
t2 123 C 12
t3 5 E 14
such that wherever table1's ReportType is t2 I want the Name and Created value from table2 for the condition table1.ResourceId = table2.Id and wherever table1's ResourceType is t3 I want the Name and Created value from table3 for the condition table1.ResourceId = table3.Id.
PS: This isn't some sort of HomeWork. I have been stuck at this query for the past 1 hour, I have read various answers and tried a few queries of my own before posting the question. Any help would really be appreciated.
Explanation in comments :)
--here we join first and second table, but we filter results to include only ReportType = t2
select t1.ReportType, t1.ResourceId, t2.Name, t2.Created from table1 as t1 join table2 as t2 on t1.ResourceId = t2.id
where t1.ReportType = 't2'
union all
--here we join first and third table, but we filter results to include only ReportType = t3
select t1.ReportType, t1.ResourceId, t3.Name, t3.Created from table1 as t1 join table3 as t3 on t1.ResourceId = t3.id
where t1.ReportType = 't3'
You can use the below query:
select report_type, resourceid,name, created from dbo.t2, dbo.t1
where report_type='t2' and ResourceId=id
UNION
select report_type, resourceid,name, created from dbo.t3, dbo.t1
where report_type='t3' and ResourceId=id;

Select records where ID is in the other table and status = 1

I need to get all the records where table1.user is in table2.userid and table2.country = 8 and table2.status = 1
This is the sample data in my database
table1
id user
-- ----
1 12
2 23
3 34
4 32
5 85
6 38
table2
id userid country_id status
-- ---- ----- ----
1 12 5 1
2 12 8 1
3 85 8 1
4 38 8 0
5 38 7 1
6 23 8 1
7 23 4 1
in this case I should only get the id #3 in table2
Inner join will do the job taking only record with match in the two tables.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 on t1.user = t2.userid
WHERE t2.country=8 and t2.status=1
You mean
"get all the records where table1.user is in table2.userid
with table2.country = 8 and table2.status = 1 ?"
If so, then:
Select * from table1 t1
Where Exists(Select * from table2
Where userId = t1.user
and country = 8
and status = 1)
Well to accomplish that is quite simple, you just need to make use of an inner join. An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
In your case, the INNER JOIN will return all rows from table2 where the join condition is met.
For a more detailed explanation and examples just visit this link: http://www.w3schools.com/sql/sql_join.asp
based on your response to Adam's approach "
I tried this but it didn't work, yes it gets the records but the id #2 in table2 is also included. what I need to get is the id #3 in table2. Thanks :) ". I changed my SQL query to:
SELECT
*
FROM
table1
INNER JOIN
table2
ON
table1.user = table2.userid
WHERE
table2.userid = 85
I assumed you only want the data of that particular userid in table2

UPDATE FROM SELECT with foreign key on parent with one query

How to update (change from first select table value second) second_table.first_table_id if first_table.email match in both select.
If it even possible. With one query!
----------------------------------------- UPDATE -----------------------------------------
EXAMPLE:
I need to update foreign key of second table if email field match in first table. I need to compare two query results with different parent_id (parents are in in same table with children)
table_1
-------------------------
| id | parent_id | email |
-------------------------
1 NULL NULL
2 NULL NULL
3 1 joe#m.ru
4 2 bob#f.ly
5 1 bob#f.ly
6 2 kira#.us
table_2
----------------
| id | first_id |
----------------
1 3
2 4
3 5
4 6
I have two parents with ids 1 and 2 and some children (ids: 3,4,5,6).
Also, keep in mind: 1 - old, 2 - new
Task: change foreign key in second table if children email with parent_id = 1 and chilren email with parent_id = 2 match (are the same).
In our example in second table row with id = 3 its foreign key field - first_id has to change from 5 to 4.
Following might get you started
UPDATE Table_2 t2u
SET first_id = (
SELECT t2.first_id
FROM Table_2 t2
INNER JOIN Table_1 t1 ON t1.id = t2.first_id
INNER JOIN (
SELECT parent_id = MAX(parent_id), email
FROM Table_1
GROUP BY
email
) t1p ON t1p.email = t1.email
INNER JOIN Table_1 t1i ON t1i.email = t1p.email
AND t1i.parent_id = t1p.parent_id
WHERE t2u.first_id <> t1i.id)
Test script (SQL Server)
;WITH Table_1 (id, parent_id, email) AS (
SELECT 1, NULL, NULL
UNION ALL SELECT 2, NULL, NULL
UNION ALL SELECT 3, 1, 'joe#m.ru'
UNION ALL SELECT 4, 2, 'bob#f.ly'
UNION ALL SELECT 5, 1, 'bob#f.ly'
UNION ALL SELECT 6, 2, 'kira#.us'
)
, Table_2 (id, first_id) AS (
SELECT 1, 3
UNION ALL SELECT 2, 4
UNION ALL SELECT 3, 5
UNION ALL SELECT 4, 6
)
SELECT t2.*, t1i.id as [update with]
FROM Table_2 t2
INNER JOIN Table_1 t1 ON t1.id = t2.first_id
INNER JOIN (
SELECT parent_id = MAX(parent_id), email
FROM Table_1
GROUP BY
email
) t1p ON t1p.email = t1.email
INNER JOIN Table_1 t1i ON t1i.email = t1p.email
AND t1i.parent_id = t1p.parent_id
WHERE t2.first_id <> t1i.id
Output
id first_id update with
----------- ----------- -----------
3 5 4