SQL select 2 tables but not ommit blank results - mysql

I have to do a select from 3 trables.
table1- idc,title,description..
table2- idc,filename,filepath,tabel1FK
table3- idc,table1FK,tabel2FK
I need a select from table1 and table2 and count unique ocurrences of table1 in table3
the select must be something like this
TB1 | TB2 | COUNT ON TB3
a | aa | 1
b | | 4
c | cc | 3
d | | 0
e | | 3

I think you just want left join and group by:
select tb1.idc, tb2.idc, count(tb3.idc)
from tb1 left join
tb2
on tb2.table1FK = tb1.idc left join
tb3
on tb3.table1FK = tb1.idc and tb3.tabel2FK = tb2.idc
group by tb1.idc, tb2.idc;

Related

How to select the ids present in another table multiple columns?

Suppose I have two tables TableA and TableB having structure as shown below:-
**TableA**
id | col_1 | col_2
1 | A | B
2 | C | D
3 | E | F
4 | G | H
**TableB**
id | TableA_first_col_id | TableA_second_col_id
1 | 1 | 2
2 | 1 | 3
Now, I want to check if TableA id is present in TableB TableA_first_col_id column or in TableA_second_col_id column.
Result should come to be
**TableA.id**
1
2
3
How can I write an optimised mysql query for this?
Try below ;
Select A.id
From TableA A
Where A.id in (select TableA_first_col_id From TableB) OR
A.id in (select TableA_second_col_id from TableB)
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
select id from tableA where exists
( select id
from tableB
where tableA.id = tableB.first_col_id or tableA.id = tableB.second_col_id
)

JOIN 2 SQL table (1 field at table 1 replace more than 1 field at the other table)

Someone please help me.
I have 2 tables, My Database is MySQL
table1
user_id | username
------- | --------
1 | joni
2 | adam
3 | jerry
4 | Dino
table2
dokumen_id | create_by | update_by | last_access_by |
doc_001 | 2 | 1 | 2 |
doc_002 | 3 | 2 | 1 |
doc_003 | 1 | 1 | 4 |
I want to join 2 tables in one query and the result like this
dokumen_id | create_by | update_by | last_access_by |
doc_001 | adam | joni | adam |
doc_002 | jerry | adam | joni |
doc_003 | joni | joni | dino |
how do i write the query?
many thanks for the help.
You need to Join table1 thrice
SELECT t2.dokumen_id,
c.username AS create_by,
u.username AS update_by,
l.username AS last_access_by
FROM table2 t2
LEFT JOIN table1 c
ON t2.create_by = c.user_id
LEFT JOIN table1 u
ON t2.update_by = u.user_id
LEFT JOIN table1 l
ON t2.last_access_by = l.user_id
Left Outer Join is used to return all the records from table2, a user might have present in create_by but not in last_access_by in that case Inner join will filter the records who is not present in create_by
Join the table1 three times with table2
select t1.dokumen_id,
tc.username create_by,
tu.username update_by,
ta.username last_access_by
from table2 t1
left outer join table1 tc
on tc.user_id = t1.create_by
left outer join table1 tu
on tu.user_id = t1.update_by
left outer join table1 ta
on ta.user_id = t1.last_access_by;
SELECT * FROM table1
LEFT JOIN table2
ON table1.user_id=table2.last_access_by;
You need to Join table1 thrice
SELECT t2.dokumen_id,
c.username AS create_by,
u.username AS update_by,
l.username AS last_access_by
FROM table2 t2
LEFT JOIN table1 c
ON t2.create_by = c.user_id
LEFT JOIN table1 u
ON t2.update_by = u.user_id
LEFT JOIN table1 l
ON t2.last_access_by = l.user_id

how to query display all data when not all related

I want to ask ..
If i have data but data related in another table
but i want the output is display all data
usually if data related in another table, I using INNER JOIN but the output just data have a relation, if dont have relation, its not display .. IF I using LEFT JOIN or RIGHT JOIN not all data displayed .. IF I using UNION data duplicated
this just example field .. field in real so many
TABLE A
ID | NAMA |
----------------------
1 | Rina |
2 | Deni |
3 | Muti |
4 | Sina |
5 | Sasa |
TABLE B
ID | Rumah |
----------------------
1 | Jabar |
2 | Jateng |
3 | Jatim |
OUTPUT THAT I WANT
ID | NAMA | Rumah
----------------------------------
1 | Rina | Jabar
2 | Deni | Jateng
3 | Muti | Jatim
4 | Sina | -
5 | Sasa | -
short version:
SELECT COALESCE(a.ID, t2.ID),
COALESCE(a.NAMA, '-')
COALESCE(b.Rumah, '-')
FROM TableA a
LEFT JOIN TableB b
ON a.ID = b.ID
RIGHT JOIN TableB t2
ON a.ID = b.ID
If I understand your problem correctly, then a given ID might only have a first or last name, but not both. In this case, simply doing a left or right join will result in the loss of data. One approach here is to do a full outer join between your two tables on the ID, and then use COALESCE to handle possibly missing data appropriately.
SELECT COALESCE(t1.ID, t2.ID) AS ID,
COALESCE(t1.NAMA, '-') AS NAMA,
COALESCE(t2.Rumah, '-') AS Rumah
FROM TableA t1
LEFT JOIN TableB t2
ON t1.ID = t2.ID
UNION
SELECT COALESCE(t1.ID, t2.ID),
COALESCE(t1.NAMA, '-')
COALESCE(t2.Rumah, '-')
FROM TableA t1
RIGHT JOIN TableB t2
ON t1.ID = t2.ID

GROUB BY in MySQL JOIN query

There is two table. First one which stores fee per user
+----+----+----+
| id |uid |fee |
+----+----+----+
| 1 |P001|100 |
+----+----+----+
| 2 |P002|200 |
+----+----+----+
| 3 |P003|250 |
+----+----+----+
| 4 |P004|100 |
+----+----+----+
| 5 |P001|200 |
+----+----+----+
| 6 |P002|200 |
+----+----+----+
| 7 |P003|250 |
+----+----+----+
| 8 |P004|100 |
+----+----+----+
second one stores user classification
+----+-----+
|uid |class|
+----+-----+
|P001| 1 |
+----+-----+
|P002| 1 |
+----+-----+
|P003| 2 |
+----+-----+
|P004| 3 |
+----+-----+
I want to show their sum grouped by class as following
1 - 700
2 - 500
3 - 200
How should I write this SQL query? Thanks in advance.
Suppose first table tab1 and second1 is tab2
select t2.class,sum(t1.fee) from tab2 t2 inner join tab1 t1 on t2.uid=t1.uid
group by t2.class
Try this
SELECT a.class, SUM(b.fee)
FROM user_classification a
INNER JOIN user_fees b
ON a.uid = b.uid
GROUP BY a.class
ORDER BY a.class ASC
Try this:
SELECT class, SUM(fee)
FROM table1 t1
LEFT JOIN table2 t2
ON t1.uid = t2.uid
GROUP BY class
SELECT COUNT(*) AS Expr28, SInv_D.fTransUnitPrice AS fTransUnitPrice, SInv_D.fExtended AS Expr2, SInv_D.fTransQty * SInv_D.fTransUnitPrice AS fTransUnitPrice,
SInv.cMode AS cMode, SInv.cInvoicNum AS cInvoicNum, SInv.dOperaDate AS dOperaDate, SInv_D.cMode AS Expr9, SInv_D.cInvoicNum AS Expr10,
SInv_D.cItCode AS cItCode, SInv_D.cItName AS cItName, SInv_D.cTransUnit AS cTransUnit, SUM(SInv_D.fTransQty) AS fTransQty, SInv.cMode AS Expr4,
SInv.cInvoicNum AS Expr16, SInv_D.cItCode AS Expr15, SInv_D.fTransUnitPrice AS Expr19, SInv.dIssueDate AS Expr20, WareDef.cCode AS Expr21,
WareDef.cName1 AS Expr22, item.cUnitR AS Expr24, item.cName1 AS Expr25, item.fCostSTDR AS Expr26, SInv.cWare1 AS Expr27, item.cCode AS cCode, I.Expr999,
I.cItCode AS Expr1
FROM SInv LEFT OUTER JOIN
WareDef ON SInv.cWare1 = WareDef.cCode LEFT OUTER JOIN
SInv_D ON SInv.cInvoicNum = SInv_D.cInvoicNum AND SInv.dIssueDate = SInv_D.dIssueDate LEFT OUTER JOIN
item ON SInv_D.cItCode = item.cCode INNER JOIN
(SELECT SUM(fTransQty) AS Expr999, cItCode
FROM dbo.sinv_D
GROUP BY sinv_D.cItCode) I ON I.cItCode = SInv_D.cItCode
WHERE (SInv.dIssueDate BETWEEN 20130102 AND 20130109) AND (SInv.cWare1 = '003') AND (SInv.cMode = '122') AND (SInv_D.cMode = '122')
GROUP BY SInv_D.cItCode, SInv_D.fTransUnitPrice, SInv_D.fExtended, SInv_D.fTransQty, SInv.cMode, SInv.cInvoicNum, SInv.dOperaDate, SInv_D.cMode, SInv_D.cInvoicNum,
SInv_D.cItName, SInv_D.cTransUnit, SInv.dIssueDate, WareDef.cCode, WareDef.cName1, item.cUnitR, item.cName1, item.fCostSTDR, SInv.cWare1, item.cCode,
I.Expr999, I.cItCode

need a help in left join query in mysql

table1:
name | map_id | reg_id
abc | 1 | 5
pqr | 2 | 5
xyz | 3 | 5
table2:
map_id | map_name | is_deleted
1 | map1 | 0
2 | map2 | 0
my sql query :
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.map_id = t2.map_id
WHERE t1.reg_id = 5
AND t2.is_deleted = 0
what the above query does is stops me from retrieving record with map_id = 3 in table1.
how can i achieve this as well as 'is_deleted check' if the record exists in table2.
thanks in advance.
You need to move the is_deleted check to the JOIN:
select t1.name, t1.map_id, t1.reg_id, -- replace select * with columns
t2.map_name, t2.is_deleted
from table1 t1
left join table2 t2
on t1.map_id = t2.map_id
and t2.is_deleted = 0
WHERE t1.reg_id = 5;
See SQL Fiddle with Demo. Your current query is causing the LEFT JOIN to act like an INNER JOIN because you have the is_deleted as a part of the WHERE clause. If you move that to the JOIN, then you will return the rows from table2 that have is_deleted=0 and you will still return all rows from table1 where reg_id=5.