SQL where exists while preserving values between them - mysql

Well, I have 2 tables like this:
Table1
ID | USER_ID
1 0
2 2
3 15
4 16
Table2
ID | FROM | TO
9 0 2
9 2 16
9 16 15
9 15 0
10 15 2
What I want is really simple but driving me crazy, considering that ID , FROM and TO represents users in table 2. I want to get someone in FROM (which is Table1.user_id) with an ID in table2 such as it also exists in TO (which is the same Table1.user_id) with the same ID of table2
For example, record 16 is eligible. Because it appears in From with ID of 9 and as TO with the same ID of 9 in table 2 (both TO and FROM correspond to a user_id of 15 in table1)
What I have done was:
select *
from `Table1`
where exists (select ID from `Table2` as p1 where FROM = 16)
and exists (select ID from `Table2` as p2 where ID = 16)
and p1.ID = p2.ID

You could try using a self join to find records with the same ID and then compare the values.
select a.from from table1 a inner table1 b on a.id = b.id
where a.from = b.to

This may work;
select * from table1 a where a.USER_ID in
(select b.FROM from table2 b
where exists (select c.id from table2 c
where b.id = c.id and b.FROM = c.TO) )

Is this what you want?
select *
from table1 t
where exists (select 1 from table2 t2 where t2.`from` = t.id) and
exists (select 1 from table2 t3 where t3.`to` = t.id);

I am not sure If understand it correctly.
If User_ID from Table_1 should be present in From AND in To columns from Table_2 AND for those records also ID in Table_2 must be same,
Then for those condition will be eligible not only User_ID 16 and 15 as you mentioned in your example but also for 0 and 2.
Assuming that is correct.
Then try this code (for mySQL you probably have to change some syntax):
SELECT A.*
FROM Table_1 AS A
INNER JOIN Table_2 AS B ON (A.USER_ID=B.FROM)
INNER JOIN Table_2 AS C ON (A.USER_ID=C.TO AND C.ID = B.ID)

Related

Join query results in MySQL

I am working in a MySQL database.
I have three tables. 1 , 2 & 3
I would like to join the result of joining tables 2 & 3 to table 1 on an id. I would like to keep all entries for table 1 and after join the result of 2 & 3 on the call id and where it doesn't match have a null value.
Table 1 has callid
Table 2 has callid and invoiceid
Table 3 has invoiceid and customerid
So join table 2 & 3 on invoiceid and filter by customerid = xyz the result of that is then joined to table 1 on callid. Table 1 would also have a Where clause filtering the on a date
result would look like this
callid customerid
123 xyz
124 xyz
125 null
126 xyz
thanking you in advance
I think you are describing left join:
select . . .
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid;
You can filter on customer id in the on clause:
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid and t3.customerid = ?;
However, this might not be exactly what you want. You might want to filter for the inner join and then do the left join:
select . . .
from table1 t1 left join
(table2 t2 join
table3 t3
on t3.invoiceid = t2.invoiceid
)
on t1.callid = t2.callid and
t3.customerid = ?

Select from 1 table sum from 2 but 1 table has a AND condition

Thanks for assisting with the previous query (SQL Query that selects a column in table 1 and uses that to select sum in table 2) of SUM from 2 tables, I now have a additional Condition for 1 of the tables. I would like to add WHERE Group1 = 1 AND IN/OUT = 'OUT'
I have 3 tables,
Names ,Groups
Names ,Payments
Names ,Payments and IN/OUT
I want to only SUM the OUT Payments in Table 3, I am getting total payments only So FAR is have:
SELECT t1.name1, SUM(t2.sale2),SUM(t3.sale3)
FROM table1 t1 JOIN table2 t2 ON t1.name1 = t2.name2
JOIN table3 t3 ON t1.name1 = t3.name3
WHERE group1 = 1
GROUP BY t1.name1
i would also like to add a zero if there is no data to sum instead of removing the whole record, Currently if a name has no payments in Table 3 but has payments in table 2 it deletes the record.
Please check the query below =>
To Get OutPayment group by Name
SELECT t1.Names,SUM(t3.Payments) As OutPayment
FROM TABLE3 as t3
INNER JOIN TABLE1 as t1 ON t1.Names = t3.Names
INNER JOIN TABLE2 as t2 ON t1.Names = t2.Names
WHERE t1.GroupID = 1 AND t3.INOROUT=2 --INOROUT =2 is OUT and 1 is IN
GROUP BY t1.Names;
To Get TotalOutPayment
SELECT SUM(t3.Payments) As TotalOutPayment
FROM TABLE3 as t3
INNER JOIN TABLE1 as t1 ON t1.Names = t3.Names
INNER JOIN TABLE2 as t2 ON t1.Names = t2.Names
WHERE t1.GroupID = 1 AND t3.INOROUT=2; --INOROUT =2 is OUT and 1 is IN
Note: Code is in DBFiddle too Check the Demo Query Link

Select only one image by its id from second table Stored procedure mysql

Table 1
id name value
1 test 22
2 test2 23
Table 2
id tb_1 pic
1 1 img.png
2 1 img2.png
3 2 img3.png
I want to select only one image from second table
Tried
SELECT a.*, b.pic
FROM tbl_one a
INNER JOIN tbl_two f
ON f.tbl_1 = (SELECT tbl_1
FROM tbl_two f2
WHERE f2.tbl_1 = a.id
GROUP BY f2.id
LIMIT 1)
how to get only one image according to tbl_1 id fro this?
I would suggest a correlated subquery:
SELECT a.*,
(SELECT f2.pic
FROM tbl_two f2
WHERE f2.tbl_1 = a.id
LIMIT 1
) pic
FROM tbl_one a;
You can use an ORDER BY in the subquery to select which pic you want -- say the earliest, most recent, or a random one.
Try this
LEFT JOIN tbl_two t2 ON (t2.id = (SELECT MIN(t2_.id )
FROM tbl_two t2_
WHERE (t2_.tb_1 = a.id )
LIMIT 1) )

Select count with join in SQL query

How can I check how many products added by a,b,c,d respectively by using a query?
table1
admin_id admin_name
3 a
4 b
5 c
6 d
table2
admin_id products
3 pDeal
3 pSeal
4 pAeal
5 pZeal
6 pXeal
3 pHeal
6 pPeal
You need a simple JOIN and a COUNT query:
SELECT table1.admin_name, COUNT(*) as cnt
FROM
table1 INNER JOIN table2
ON table1.admin_id = table2.admin_id
GROUP BY
table1.admin_name
Try this...
SELECT a.admin_name, COUNT(b.products) as 'CountOfProducts'
FROM table1 a INNER JOIN table2 b ON a.admin_id = b.admin_id
GROUP BY a.admin_name
Use this
SELECT adm.admin_name,COUNT(pdr.products) as ProductCnt
FROM table1 AS adm
JOIN table2 AS pdr
ON adm.admin_id = pdr.admin_id
GROUP BY adm.admin_id;
SELECT t1.admin_name, COUNT(t2.products)
FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.admin_id = t2.admin_id
WHERE 1
GROUP BY t2.admin_id
The LEFT JOIN will ensure the cases when table2 doesn't have any record for some admin of table 1.
You can use an inner-select like this:
SELECT
table1.admin_name,
(SELECT COUNT(*)
FROM table2
WHERE table1.admin_id = table2.admin_id) As cnt
FROM
table1;

three tables with Inner Join

I have the following three tables...
Table1
IDA colB colC
111 a w
222 b w
333 c s
444 b g
Table2
IDB colB colC
11 w f
12 w r
13 s g
Table3
IDA IDB
111 11
222 12
333 13
444 14
What I need is to copy from table1 to table2 and I could use the following easy MySQL query to do that...
INSERT INTO table2 SELECT * FROM table1
The problem is I don't the same id type,...the two tables are connected over the third table table3.
in which IDA contains table1 primary key and IDB contain table2 primary key,
so, example if I want to copy from table1 IDA(111) to table2 how do I do that?
and if the IDB exists how do I update on Duplicate Key...
I have the following query but no working...
INSERT INTO table2 SELECT * FROM table1
WHERE IDA IN ( SELECT table1 b
INNER JOIN table3 c ON c.IDA = b.IDA
INNER JOIN table2 a ON a.IDB = c.IDB )
WHERE b.IDA=111
But, I wish if I get generalize answer...Thanks
INSERT INTO table2
SELECT
t3.idb
,t1.colb as ncolb
,t1.colc as ncolc
FROM
table1 t1
join table3 t3
on t1.ida = t3.ida
ON DUPLICATE KEY UPDATE
colb = ncolb
,colc = ncolc
No MySQL on me right now so syntax might not be 100% correct, but this should give you the idea of how it should be done.
Depending on whether table3 has entry for each table1 id you might need to change t3.idb to coalesce(t3.idb, t1.ida) and change join to left join in the query if you want them to be copied. Remember that table2 will then have ids from table1)
INSERT INTO table2 SELECT * FROM table1
WHERE IDA IN ( SELECT * FROM table1 b
INNER JOIN table3 c ON c.IDA = b.IDA
INNER JOIN table2 a ON a.IDB = c.IDB ) HAVING b.IDA=111