How to get row data based on the other table row data? - mysql

I'm not sure if I write my questions title correctly, but my example would probably explain it better.
Join table_1 and table_2 and get table_1 row, if there is no is_validated= 1.
The query will get table_1, id 1 and 3, because they dont have is_validated = 1
table_1
id
1
2
3
table_2
id table_1_id is_validated
1 1 0
2 2 1
3 2 0
4 3 0
5 3 0
expected result
id
1
3
I've try to use this query, but it doesn't get the result that I want.
SELECT * FROM table_1 t1
JOIN table_2 t2 ON t1.id=t2.table_1_id AND t2.is_validated = 0
GROUP BY t2.table_1_id

Exists logic works nicely here:
SELECT t1.id
FROM table_1 t1
WHERE NOT EXISTS (SELECT 1 FROM table_2 t2
WHERE t2.table_1_id = t1.id AND t2.is_validated = 1);

Related

Correlated subquery & conditional where clause in Sequelize

I have a correlated subquery that is validated based on the column value. Even though I read through the sequelize docs and examples on GitHub, I did not really get any working solutions.
Query I am trying to execute
select count(*) from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
Any references or suggestions are appreciated.
Update:
The goal is to fetch all records from table1, but if deciding_col value is true for a record, then I need to check if that particular record exist in table2.
Example:
Table1 Data:
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
Table2 Data:
id | second_condition
1 132
Output:
1.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
2.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=133)
else 1=1 end;
id | name | deciding_col
2 edfg 0
3 xyz 0

MYSQL: Help for fast update

i need some help to get a fast update on my table in MySQL
Table 1
id | value
1 0
2 0
3 0 ...
Table 2
t1_id | t2_id
1 2
1 3
3 5 ...
Have about 150,000 rows in table 1, and about 1,3 million in table 2. I need set t1.value = 1 when t1.id exists in table 2.
update table1 t1, table2 t2
set value = 1
where t1.id = t2.id;
Without some distinct parameter, it will do many times for each id, making it slow to update all t1 rows.
Any help would be gladly accepted.
what about:
UPDATE t1
SET t1.value = 1
FROM table_t1 t1
WHERE EXISTS (SELECT 1
FROM table_t2 t2
WHERE t2.id = t1.id
)
what about:
update table1
set value=1
from table2
where table1.id=table2.t1_id

Mixing 3 sql queries in 1

I have 3 tables as follows
Table1
Id Name
1 abcd
2 bcd
3 dabc
Table2
Id2 Name2
2 xyz
3 def
4 mno
Table3
Id Id2 Value
1 4 1
2 3 1
3 4 1
Now,
From table1 : I have to select all Id where Name is %abc%
From table2: I have to select Id2 where Name2 is "mno"
From Table3: I have to change value to 0 from 1 where Id's value are from Table1 and Id2 is from Table2.
Table 1:
select Id from Table1 where Name like '%abc%'
Table2 :
select Id2 from Table2 where Name2 = "mno"
Table 3:
update Table3 set Value = 0 where Id in() and Id2=
But, I dont know how to make it 1 single query. Can anyone please guide me up ?
Refer to: prior stack article
You've not explained how T1 relates to T2, So I have assumed a cross join.
Whenever you have a record in T1 with name like '%abc%' (1,3) in your data..
and whenever you have a record in T2 with a name equal to 'mno' 4 then you want the value in table 3 to be 0
so the select we generate should produce
1,4
3,4
and when we inner join this back to table 3 it only selects
Id Id2 Value
1 4 1
3 4 1
Now we generate an update based on this select as outlined in the link provided above...
UPDATE table3
INNER JOIN (
SSELECT t1.ID t1ID, t2.id t2ID
FROM table1 t1
CROSS JOIN table2
WHERE t1.name like '%abc%'
and t2.name like = 'mno') B
on B.t1ID = t3.Id
and B.t2ID = T3.ID2
SET value = 0
Giving us a result of
Id Id2 Value
1 4 0
2 3 1
3 4 0
if we select * from table3
update t3
set t3.Value = 0
from Table3 t3
inner join Table1 t1
on t3.Id = t1.Id
inner join Table2 t2
on t3.Id2 = t2.Id2
where t1.Name like '%abc%' and t2.Name2 = 'mno'
OR
update Table3
set value = 0
where Id in (select Id from Table1 where Name like '%abc%')
and Id2 in (select Id2 from Table2 where Name2 = 'mno')
You should think about UPDATE ... WHERE EXISTS as follows:
update Table3 set Value = 0
WHERE EXISTS (SELECT 1 FROM Table1 where Name LIKE '%abc%' AND Table1.Id=Table3.Id )
AND EXISTS (SELECT 1 FROM Table2 where Name2 = "mno" AND Table2.Id2=Table3.Id2)

Mysql left join fails to returns all values from first table

I've two tables named table1 and table2. table2 may have elements from table1. I'd like to show all the results from table2 with the price if available in table1 and with status 1. If no product matches in table1 and also status is 0, need to return price as 0.
table1
id pname item_code price status
1 product1 abcd 200 1
2 product2 pqrs 500 1
3 product3 wxyz 425 1
4 product5 mnop 100 0
and table2 as follows
id item_code
10 efgh
11 abcd
12 pqrs
13 mnop
I have tried following query
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN `table1` as t1 ON `t1`.`item_code` = `t2`.`item_code` WHERE `t1`.`status` = 1 GROUP BY `t2`.`item_code`
but it returns common values in table1 and table2 with status 1, but I need all records from table2 with price as 0 if nothing match in table1 or status 0 in table1.
Expected output
id item_code price
10 efgh 0
11 abcd 200
12 pqrs 500
13 mnop 0
Any help please.
Thanks,
Not sure about your current query which is having count and group by however you can do as below
select
t2.id,
t2.item_code,
case
when t1.status = 1 then t1.price
else 0
end as price
from table2 t2
left join table1 t1 on t1.item_code = t2.item_code
Now note that if table1 has multiple matching values from table2 in that case we may need grouping data.
Try below query:
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN (select * from table1 where status = 1)t1 ON `t1`.`item_code` = `t2`.`item_code` GROUP BY `t2`.`item_code`
Move the part of the WHERE clause that checks the left joined table to the ON clause
SELECT `t2`.`id`, `t2`.`item_code`, COALESCE(`t1`.`price`, 0),`t1`.`pname`, COUNT(t2.item_code) AS sellers
FROM (`table2` as t2)
LEFT JOIN `table1` as t1
ON `t1`.`item_code` = `t2`.`item_code`
AND`t1`.`status` = 1
GROUP BY `t2`.`item_code`

How to select a column from one table and two columns from another table when both the tables have no relation in T-SQL?

I will explain my problem as simple as possible.
I have written a select query Query1 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=1
ID Column1 Column2 TypeID
1 A A 1
2 B B 1
3 C C 1
I have another table Table2 which has data in the following format
ID Column1 Column2
1 0 0
1 1 1
2 2 2
2 3 3
2 4 4
3 5 5
3 6 6
I have written a another select query Query2 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=2
ID Column1 Column2 TypeID
4 A A 2
5 B B 2
6 C C 2
Table1 and Table2 have no column in common and data in Column1 and Column2 for both TypeID's in Table1 is same and currently data in Table2 has data with values of ID column from Table1 for id's 1,2,3 only and i want to write a select query to select same data from Table2 but with values of ID column from Table1 with TypeID as 2 which I have given below
ID Column1 Column2
4 0 0
4 1 1
5 2 2
5 3 3
5 4 4
6 5 5
6 6 6
how can I achieve this by writing a select query in sql server?
select T1.column1, isnull(T2.column2,0) as column2,
isnull(T2.column3,0) as column3
from table1 T1
left outer join on Table2 T2 on T1.column9=T2.column9
where ....
If I understood the question correctly, the query whould be like that:
SELECT
T_ID.T2_ID,
Column1,
Column2
FROM
Table2
JOIN
(
SELECT
T1.ID AS T1_ID,
T2.ID AS T2_ID
FROM
Table1 AS T1
JOIN Table1 AS T2 ON T1.Column1 = T2.Column1 AND T1.Column2 = T2.Column2
WHERE
T1.TypeID = 1
AND T2.TypeID = 2
) AS T_ID ON Table2.ID = T_ID.T1_ID