Correlated subquery & conditional where clause in Sequelize - mysql

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

Related

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

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);

create view joining two tables having same columns without duplicates

Can anyone please help me on how to create view joining two tables having same columns without duplicates?
Example:
I have two tables T1 and T2
T1
Id Name Date
-----------------------
1 AAA 2019-04-05
2 BBB 2019-04-06
3 CCC 2019-04-07
T2
Id Name Date
----------------------
4 DDD 2019-04-01
1 ABC 2019-03-01
2 DEF 2019-03-02
My output view should look like this
Id Name Date
------------------------
1 AAA 2019-04-05 (From T1)
2 BBB 2019-04-06 (From T1)
3 CCC 2019-04-07 (From T1)
4 DDD 2019-04-01 (From T2)
Below is the query I am trying
CREATE VIEW view AS (
(
SELECT
t1.id,
t1.name,
t1.date,
FROM
T1 as t1
UNION
SELECT
t2.id,
t2.name,
t2.date,
FROM
T2 as t2
)
But I get duplicate records.
You seem to want a prioritization query, with table1 prioritized over table2. In MySQL:
select t1.id, t1.name, t1.date
from table1 t1
union all
select t2.id, t2.name, t2.date
from table2 t2
where not exists (select 1 from table1 t1 where t2.id = t1.id);
This selects all rows from table1 and then selects all rows from table2 that don't have a matching id in table1.
This assumes that id is sufficient for determining which records are "duplicates".

Using CASE in Mysql to set value to an Alias field

I have 2 tables in Mysql which looks as follows
Table 1
ID YEARMONTH
------------------
1 201210
2 201211
3 201212
Table2
ID YEARMONTH GRADE DESIG
--------------------------------
1 201210 G1 NULL
2 201210 G1 D2
3 201212 G1 D1
I am trying to set a new field (named FLAG) in table1 which will be Y if rows exist for t1.YEARMONTH in T2 else N
I want the following Result in T1
ID YEARMONTH FLAG
---------------------
1 201210 Y
2 201211 N
3 201212 Y
I have tried the following Query
SELECT
T1.ID,
T1.YEARMONTH,
CASE COUNT(T2.ID)
WHEN (SELECT
COUNT(T2.ID)
FROM TABLE2 T2)
> 0 THEN 'Y'
ELSE 'N'
END AS flag
FROM TABLE1 T1,
table2 T2;
But it produces the following output
ID YEARMONTH FLAG
---------------------------
1 201210 N
I don't know where my mistake lies. Any help would be appreciated.
Try using the below query:
SELECT
T1.ID, T1.YEARMONTH,
(CASE
WHEN COUNT(T2.ID) > 0 THEN 'Y'
ELSE 'N'
END) AS FLAG
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
ON T1.YEARMONTH = T2.YEARMONTH
GROUP BY T1.ID, T1.YEARMONTH
You can see the required output here http://sqlfiddle.com/#!9/162855/12
TRY THIS: Using CASE with LEFT JOIN you can check the existence of t1.yearmonth in t2.yearmonth and generate the flag column accordingly
SELECT DISTINCT t1.id,
t1.yearmonth,
(CASE WHEN t2.yearmonth IS NOT NULL THEN 'y' ELSE 'n' END) flag
FROM table1 t1
LEFT JOIN table2 t2 ON t1.yearmonth = t2.yearmonth

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)