MySQL query for not exists in another table - mysql

I have two MySQL Tables...
1. master_fee
2. fees_receiving_ledger
I do not want to show the FeeId containing FeeFrequencyId : 4 from master_fee table if they are present in fees_receiving_ledger table.
Like above I do not want to show the Admission Fee (FeeId:1) containing FrequencyId : 4 as it is present in fees_receiving_ledger.
I have tried like below...
Select
master_fee.*
From
master_fee
Where Not Exists(Select
fees_receiving_ledger.FeeId
From
fees_receiving_ledger
Where
fees_receiving_ledger.FrequencyId = '4')
My query giving me blank result.
I tried This but failed.
What should be the query ?
I am using VB.NET with MySQL database.

you forgot the join condition in your subselect.
select m.* from master_fee m
where not exists (
select 1 from
fees_receiving_ledger f
where f.frequencyID = 4
and m.feeid=f.feeid)
You could also do:
select m.* from master_fee m left join fees_receiving_ledger f
on f.feeid = m.feeID
where f.frequencyID <> 4;

Related

Count field and group by regarding another field on same table MySQL

I am trying to find count of field by another field in same table on MySQL.My Table Like This:
Id DrgId CodeType IsPrincipal Code
182250051 48261836 I 1 T151
182250055 48261836 I 2 U739
182250059 48261836 I 3 Y929
182250061 48261836 I 4 W444
182250062 48261836 A 2 3006104
So I want to find used helper codes for T151 which is IsPrincipal equals 1.
Output should like this:
Code Helper_I_Count Helper_A_Count
T151 3 1
So I tried Like this:
SELECT t.`Code`,COUNT(v1.`Code`) AS EkTaniSay,COUNT(v2.`Code`) AS IslemSay
FROM TIGPatientCode t,
(
SELECT DRGPatientId,`Code`
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='I'
) v1,
(
SELECT DRGPatientId,`Code`
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='A'
) v2
WHERE t.IsPrincipal='1' AND t.DRGPatientId=v1.DRGPatientId AND t.DRGPatientId=v2.DRGPatientId
GROUP BY t.`Code`
But it wont get actual count.
How can I Do this?
Thanks
SELECT t2.Code,
SUM(t1.CodeType = 'I') AS EkTaniSay,
SUM(t1.CodeType = 'A') AS IslemSay
FROM TIGPatientCode AS t1
RIGHT JOIN TIGPatientCode AS t2 ON t1.DrgPatientId = t2.DrgPatientId
WHERE t1.isPrincipal != 1 AND t2.isPrincipal = 1
GROUP BY t1.DrgPatientId;
The first part of the query is based on multiple query same table but in different columns mysql. Then I join this with the table again to get the code for the principal row.
The problem with your query is that joining the two subqueries creates a cross-product of all the rows, which causes the counts to be multiplied. Also, if there's any group that doesn't have one of the codes, that subquery will return no rows, so the join will be empty for that code. You could fix the first problem by doing the counts in the subqueries rather than the main query. The second problem can be fixed by using LEFT JOIN. So the fixed version of your query would look like:
SELECT t.Code, v1.EkTaniSay, v2.IslemSay
FROM TIGPatientCode t
LEFT JOIN (
SELECT DRGPatientId, COUNT(*) AS EkTaniSay
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='I'
GROUP BY DRGPatientId
) AS v2 ON t.DRGPatientId = v2.DRGPatientId
LEFT JOIN (
SELECT DRGPatientId, COUNT(*) AS IslemSay
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='A'
GROUP BY DRGPatientId
) AS v1 ON t.DRGPatientId = v1.DRGPatientId
WHERE t.IsPrincipal = 1
DEMO

(another) LEFT JOIN issue with ACCESS

I want summary data from my 'Data' table for all companies in 'Companies' table including blank rows where there is no record in Data.
If I summarise the data in a nested SELECT clause (or in a stored query i get nothing from the data table. For example
This is the sub select
SELECT transco,
sum(m1) AS Jan15,
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco
which outputs:
transco Jan15
0292 154373665
1419 134915098
If I use it in a sub select as follows
SELECT c.SAP_Code,
Jan15
FROM Companies AS c
LEFT JOIN (
SELECT transco,
sum(m1) as Jan15
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco)
AS d
ON c.SAP_Code = d.transco
I get:
SAP_Code Jan15
0292
1419
I can get the correct result via a temporary table:
SELECT sum(m1) as Jan15,
transco
INTO Temp_Table
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco
followed by
Select c.SAP_code,
jan15
FROM companies AS c
LEFT JOIN Temp_Table as i
ON (c.SAP_Code = i.transco)
giving:
SAP_code jan15
0292 154373665
1419 134915098
1423
but if I use temporary tables I will have to create macros and i want users to be able to run just a query.
The following works for this simple case but I can't apply it in other circumstances:
SELECT c.SAP_Code,
sum(m1) AS Jan15
FROM Companies AS c
LEFT JOIN data as d
ON c.SAP_Code = d.transco
WHERE (d.QVmeasure = 'Vol') OR (d.QVmeasure is null)
GROUP BY d.QVmeasure,c.sap_code
Is there something wrong with my sub select syntax or is it ACCESS (2013)
TIA
You could nest the sub-query like this:
SELECT c.SAP_Code,
(SELECT sum(d.m1)
FROM data AS d
WHERE d.QVmeasure = 'Vol' AND c.SAP_Code = d.transco
) AS [Jan15]
FROM Companies AS c

Mysql check if row exists in regular select?

I got an question about mysql. Is it possible to check if an row exists in an external table?
For example, I need to show recent uploaded images and I would like to show some userdata with it and show if the user is online.
My current query is:
SELECT `users`.`username`, `users`.`location`, `users`.`age`, `users`.`dateofbirth`
FROM (`images`)
JOIN `users`
ON `users`.`userid` = `images`.`userid`
WHERE `images`.`active` = 1
LIMIT 12
But I'm missing the 'is online' part. Is it possible to use this in the select? For example if the row exists there would be a value in the select named online and it's value = 1 and if user is not online (no row in table) the value needs to be 0. Is that possible?
Seems like you just need to use a LEFT JOIN on the other table and then a CASE to provide the value:
SELECT `users`.`username`,
`users`.`location`,
`users`.`age`,
`users`.`dateofbirth`,
case when o.`userid` is null then 0 else 1 end UserOnline
FROM (`images`)
JOIN `users`
ON `users`.`userid` = `images`.`userid`
left join online_table o
on `users`.`userid` = o.`userid`
WHERE `images`.`active` = 1
LIMIT 12

mysql select from 3 tables not working

I have 3 tables on my database with the following data:
tb_collection: id_collection, name
tb_collection_product: id, id_collection, id_product
tb_product: id_product, photo
Here's what I'm trying to do:
Select the last 2 products of each collection...I've tried group by with no luck.
Just got it working doing the following:
Added a new column called "show" as a tinyint field with default 0 and 1 for selected.
Then, the following mysql:
SELECT tb_product.photo, tb_collection_product.* FROM tb_product,tb_collection_product WHERE tb_collection_product.show=1 AND tb_collection_product.id_product = tb_product.id_product
try this query
SELECT tb_collection.id_collection,tb_collection.name ,
tb_collection_product.id , tb_collection_product.id_collection,
tb_collection_product.id_product, tb_product.id_product,
tb_product.photo
FROM tb_collection
INNER JOIN tb_collection_product
ON tb_collection.id_collection=tb_collection_product.id_collection
INNER JOIN tb_product
ON tb_collection_product.id = tb_product.id_product
ORDER BY tb_collection.id_collection DESC limit 2

MySQL - Using column value for joining in the same query

I have three tables that looks something like this:
Table joins
|ID|JOIN_NAME|
1 persons
2 companies
Table information
|ID|JOIN_ID|
1 1
2 2
Table information_extra_persons
|ID|INFORMATION_ID|NAME|
1 1 John
Table information_extra_companies
|ID|INFORMATION_ID|NAME|
1 2 IBM
How can i join together these tables in one SQL? I've tried something like:
SELECT * FROM `information`
INNER JOIN `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`
ON `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`.`information_id` = `information`.`id`
but I can't get it to work. Of course this isn't my actual table setup, but it's the same principle. Does anyone know how to get all the info in just one SQL?
That's actually four tables, not three. This isn't just a nitpick - it looks as though the substance of your question is "how can I use the name of the table as part of the join criteria?" (ie. how can the information_extra_ tables be treated as a single table?)
To which the answer is: you can't. (Outside of dynamic SQL.)
In this specific case, the following should return what I think you are looking for:
select j.join_name joined_entity,
case when j.join_name = 'persons' then p.name
else c.name
end joined_entity_name
from information i
inner join joins j on i.join_id = j.id
left join information_extra_persons p on i.id = p.information_id
left join information_extra_companies c on i.id = c.information_id
Alternatively, a less efficient (but more general) approach might be:
select j.join_name joined_entity,
v.name joined_entity_name
from information i
inner join joins j on i.join_id = j.id
inner join (select 'persons' entity, information_id, name from information_extra_persons
union all
select 'companies' entity, information_id, name from information_extra_companies) v
on i.id = v.information_id and j.join_name = v.entity