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
Related
Goal:
Trying to join together two tables
Table structure:
t1:
name | id
t2:
id_a | id_b | id_c | id_d | favorite color
Problem:
I'm trying to find out the favorite color that corresponds to each name, where the t1.id is found in 1 of the 4 id fields in t2. The tricky part is that the non-matching values aren't null, so a coalesce doesn't work.
What I've tried:
Tried a case when statement in the join, but that seems to be creating some endless loop that is never finishing.
Trying a union, but that is creating some unexpected duplication.
Also tried a multi- on condition (like below), but that's not working:
WITH test AS (
SELECT
t1.*
, t2.*
FROM t1
LEFT JOIN t2
ON ( t1.id = t2.id_a
OR t1.id = t2.id_b
OR t1.id = t2.id_c
OR t1.id = t2.id_d
)
)
SELECT COUNT(*) FROM test
;
Here's an example dataset:
WITH names AS(
SELECT
1 as id , 'alfred' as name
UNION ALL SELECT 2, 'becca'
UNION ALL SELECT 3, 'charlie'
UNION ALL SELECT 4, 'dezi'
)
, color AS(
SELECT
1 as id_a, 6 as id_b, 9 as id_c, 7 as id_d, 'green' as fave_color
UNION ALL SELECT 1,2,6,5, 'orange'
UNION ALL SELECT 5,7,9,3, 'blue'
UNION ALL SELECT 9,4,6,8, 'black'
)
SELECT
n.id
, n.name
, c.fave_color
FROM color c
LEFT JOIN names n
ON n.id IN (c.id_a,c.id_b,c.id_c,c.id_d)
GROUP BY 1,2,3
ORDER BY 1,2
;
I have two tables like below
Table 1 :
Id User_id rating1 rating2 rating3
1 4 4 3 5
2 4 5 2 2
3 1 5 5 5
4 4 1 2 3
Table 2 :
Id User rating1 rating2 rating3 comment
1 1 5 2 1 okay
2 4 2 4 4 good
I want to get the count of user_id who having rating1 value between 1 , 2 , 3, 4, 5 and who having rating2 value between 1 , 2 , 3, 4, 5 and who having rating3 value between 1 , 2 , 3, 4, 5
I am trying the following way ,
select count(Table1.User_id)
from Table1
join Table2
on Table1.User_id = Table2.User
where Table1.rating1 = ( 1 || 2 || 3 || 4 || 5)
and Table2.rating1 = ( 1 || 2 || 3 || 4 || 5 )
But it's not working.
based on your comment, your query is very far from your desire result.
You need to pivot your table to be a single column
SELECT rating, count(*)
FROM (
SELECT `User_id` , `rating1` as rating FROM Table1
UNION ALL
SELECT `User_id` , `rating2` as rating FROM Table1
UNION ALL
SELECT `User_id` , `rating3` as rating FROM Table1
UNION ALL
SELECT `User` , `rating1` as rating FROM Table2
UNION ALL
SELECT `User` , `rating2` as rating FROM Table2
UNION ALL
SELECT `User` , `rating3` as rating FROM Table2
) as combine
GROUP BY rating
ORDER BY rating
Use IN (1,2,3,4,5) Insted of = (1,2,3,4,5)
It will give error of multiple oprands since equal can be use when it comes to compare one on one value.
SELECT t1.user_id,t2.user,count(*) as Count_Rate
FROM
(SELECT * FROM table1 WHERE rating1 IN ('1','2','3','4','5')) as t1
JOIN
(SELECT * FROM table2 WHERE rating1 IN ('1','2','3','4','5')) as t2
on t1.user_id = t2.user
GROUP by user_id
Or You can try this also by adding,
(SELECT *,count(rating1='1') AS rate1 FROM table1 WHERE rating1 IN ('1','2','3','4','5')) as t1
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);
I have 2 tables called table_one and table_two, with the following properties:
Both of them have one common column called column_id.
The values of table_one.column_id are unique, whereas the values of table_two.column_id are not.
table_two has two extra columns called ts_one and ts_two. ts_one is not null, but ts_two may be null. Only one row per table_two.column_id permits the ts_two value to be null.
Not all values from table_one.column_id may be presented in table_two.column_id.
For example:
table_one
column_id
1
2
3
4
5
table_two
column_id ts_one ts_two
2 2014-10-01 null
3 2014-10-02 2014-10-03
3 2014-10-05 null
4 2014-10-01 2014-10-05
I need to get all id from table_one.column_id, where:
id in table_one.column_id and not in table_two.column_id(1, 5 satisfy that)
id in table_one.column_id and in table_two.column_id where ts_two is not null and there is no another rows with same id where ts_two with null value - (only 4 satisfy).
Both conditions should be taken into account. The results should include 1, 4, and 5.
You can do this with a single join and an aggregate...
SELECT t1.column_id FROM table_one AS t1
LEFT JOIN (
SELECT column_id, (COUNT(column_id) = COUNT(ts_two)) as no_nulls FROM table_two
GROUP BY column_id
) AS t2
ON t1.column_id = t2.column_id
WHERE t2.column_id IS NULL OR (t2.column_id IS NOT NULL AND t2.no_nulls = TRUE)
SQLFiddle: http://sqlfiddle.com/#!2/14855/8/0
Query everything from table 1
Query each id from table 2, along with whether it has any null ts_two entries for that id.
Joins them together if 1 is missing from 2, or if 1 is in 2 and has no null ts_two entries.
Updated Fiddle: http://sqlfiddle.com/#!2/0b39d2/15/0
SELECT *
FROM Table_one A
LEFT JOIN Table_Two B
on A.Column_ID = B.ColumN_ID
LEFT JOIN (Select column_ID
From table_two
where ts_one is null or ts_two is null) C
ON A.ColumN_Id = C.Column_ID
WHERE C.Column_ID is null
OR B.Column_ID is Null;
What this does:
Returns all data from table 1 in Set 1
Joins to data from table two which are not in 1,5 (Adds data to set 1)
Joins to data from table two that has been filtered to only include records with nulls (creates a 2nd set)
Excludes records where matches exist in 2nd join to table two. (Filters data so that only data in first set and not in second set is returned.
for your first question " id in table_one.column_id and not in table_two.column_id(1, 5)
"
this query should do
SELECT column_id
FROM table_one AS o
WHERE NOT EXISTS (SELECT 1 FROM table_two WHERE column_id = o.column_id)
for your second question "id in table_one.column_id and in table_two.column_id where ts_two is not null and there is no id with a null value - (only 4)"
this query should also do
SELECT t.* FROM table_two AS t
INNER JOIN table_one AS o ON o.column_id = t.column_id
WHERE t.ts_two IS NOT NULL AND NOT EXISTS (SELECT 1 FROM table_two WHERE ts_two IS NULL AND column_id = o.column_id)
the question is so confusing. but from the comments below this should give you what you need
SELECT DISTINCT column_id
FROM (
SELECT column_id
FROM table_one AS o
WHERE NOT EXISTS (SELECT 1 FROM table_two WHERE column_id = o.column_id)
UNION
SELECT t.column_id FROM table_two AS t
INNER JOIN table_one AS o ON o.column_id = t.column_id
WHERE t.ts_two IS NOT NULL AND NOT EXISTS (SELECT 1 FROM table_two WHERE ts_two IS NULL AND column_id = o.column_id)
) AS t
I have a table as below
ID | CID
1 | 3
2 | 0
3 | 4
4 | 0
5 | 0
6 | 3
Below is the SQL query I use which is SELF JOIN.
SELECT t1.ID
FROM `tbl_a` AS t1 JOIN `tbl_a` AS t2
ON t1.ID = t2.CID
Which gives me O/P as below.
ID | CID
3 | 4
4 | 0
But what I want as an O/P is 1,3,4,6.
Logic of the O/P is Rows IDs or CIDs which are being used. If I explain more When the ID is 1 CID 3, When the ID is 3 CID is 4, When the ID is 6 CID is 3. When I get the unique IDs & CIDs that are used in the table would be 1,3,4,6.
Final Correct O/P Required is below.
ID
1
3
4
6
How can I get it done?
Not sure what you're trying to do. I think you are saying you want the ID of rows that have a non-zero CID or that are referenced by the CID column. (?) Try this:
SELECT ID FROM tbl_a AS t1 WHERE CID <> 0 OR EXISTS(SELECT * FROM tbl_a AS t2 WHERE t2.CID = t1.ID) ORDER BY ID
Try this
SELECT t2.ID
FROM `tbl_a` AS t1 JOIN `tbl_a` AS t2
ON t1.ID = t2.CID
OR t2.ID = t1.CID
GROUP BY t2.ID
I think this may be what you want:
select ID
from tbl_a
where id in (3, 4) or cid in (3, 4);