Combining EXISTS and LEFT JOIN - mysql

I need some help combining two queries I wrote( I do not know if it is possible to do it or not). But first let me show you the table and exaplin it so there are no ambiguous angles here.
This is the table I have (PS : I do not know how to make decent looking tables in StackOverflow even though I researched it, and tried to use Senseful solutions so please excuse the image) :
Main Table
The first query I have is the following :
SELECT *
FROM Dropship As t1
WHERE t1.HUB_SO_GOODS_ISSUE_DATE IS NULL
AND EXISTS (SELECT * FROM Dropship t2
WHERE t2.LE_PO = t1.LE_PO
AND t2.HUB_SO_GOODS_ISSUE_DATE IS NOT NULL);
This query gives me all orders that have not have been fully processed. So with the table I have it gives me the orders (LE_PO) 300 and 500 like in the following image :
result from first query
Another query I use is the left join one :
SELECT Dropship.*, Notes_Replenishment.*
FROM Dropship LEFT JOIN Notes_Replenishment ON Dropship.LE_PO = Notes_Replenishment.LE_PO;
The notes_replenishment table has all the orders (LE_PO) but also comments put in by a user. What I would like to do is to incorporate the left join in to the first query so that it gives me the result (see above) but also the comments from the Notes_replenishment table however I get errors when I tried doing it by myself.
Could somebody give me some pointers on how to combine the two queries?
Thank you all in advance!

SELECT *
FROM Dropship As t1
LEFT JOIN
Notes_Replenishment
ON t1.LE_PO = Notes_Replenishment.LE_PO
WHERE t1.HUB_SO_GOODS_ISSUE_DATE IS NULL
AND EXISTS
(
SELECT *
FROM Dropship t2
WHERE t2.LE_PO = t1.LE_PO
AND t2.HUB_SO_GOODS_ISSUE_DATE IS NOT NULL
)

Related

Why are my records blank in my Access query?

I'm trying to run this query:
SELECT tbl_G_ov_wta.PK_G, qry_performPrepElo_wta.PK_G,
qry_performPrepElo_wta.ID1_ocStake
FROM tbl_G_ov_wta
INNER JOIN qry_performPrepElo_wta
ON tbl_G_ov_wta.PK_G = qry_performPrepElo_wta.PK_G
For some reason it won't pull through the values of qry_performPrepElo_wta.ID1_ocStake. Worth mentioning that PK_G is a common primary key. In playing around with this I've found that adding in criteria as follows fixes the issue:
SELECT tbl_G_ov_wta.PK_G, qry_performPrepElo_wta.PK_G,
qry_performPrepElo_wta.ID1_ocStake
FROM tbl_G_ov_wta
INNER JOIN qry_performPrepElo_wta
ON tbl_G_ov_wta.PK_G = qry_performPrepElo_wta.PK_G
WHERE (((tbl_G_ov_wta.PK_G) Between 1 And 1000000));
Any ideas on why this might be?

Query returning data about nested elements

I have three tables in my database:
boards: idBoard, nameBoard, author
sections: idSection, nameSection, idBoard
tasks: idTask, nameTask, idSection
My query looks like this:
SELECT tasks.idTask, sections.idSection, (more...)
FROM `tasks`
INNER JOIN `boards` ON tasks.idBoard = boards.idBoard
INNER JOIN `sections` ON sections.idSection = tasks.idSection
WHERE boards.idBoard = ?
AND boards.author= ?
The query works almost well, but it doesn't return sections that don't have their tasks items.
Unfortunately, I don't really know why it works like that. I would also like to get section items matching boards but not having tasks items
I think you want outer joins, which usually means left join. Based on the way you describe your tables, I would expect the query to look like this:
SELECT t.idTask, s.idSection, (more...)
FROM boards b LEFT JOIN
sections s
ON b.idBoard = s.idBoard
tasks t
ON s.idSection = t.idSection
WHERE b.idBoard = ? AND boards.author= ?
I think you made a mistake in your query. tasks table doesn't have a column called 'idBoard' in your post.
How about this?
SELECT tasks.idTask, sections.idSection, (more...)
FROM `tasks`
INNER JOIN `sections` ON sections.idSection = tasks.idSection
INNER JOIN `boards` ON sections.idBoard = boards.idBoard
WHERE boards.idBoard = ?
AND boards.author= ?
Could you explain more about what you want?

How can I compare the sum of one field to another single field?

My data set is like so:
Total_Order_Table
Order_no (unique)
Shipped_quantity
Order_Detail_Table
Order_number (not unique)
Quantity_per_bundle
I need to take the sum of Quantity_per_bundle for each order_number from Order_Detail_Table and compare it to the Shipped_quantity.
My idea is an outer join so that my data will look like so:
I need to be able to see quantity discrepancies and if the order number exists in both tables.
Thanks in advance!
Normaly with a FULL OUTER JOIN in sql:
SELECT to.Order_no AS Order_no_Total_Order_Table, od.Order_number AS Order_No_Ordr_detail_Table, SUM(od.Order_number) AS sum_Quanitty_Per_Bundle, od.Order_number
FROM Total_Order_Table AS to
FULL OUTER JOIN Order_Detail_Table AS od ON to.Order_no = od.Order_number
GROUP BY to.Order_no
But FULL OUTER JOIN don't exist in mysql. But you can simulate it : http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Unless I'm missing something subtle in the question, isn't this just as simple as a SELECT query with a join between the tables.
Something along the lines of this should achieve the result:
SELECT tot.Order_no,
odt.Order_no,
SUM(odt.Quantity_per_bundle),
tot.Shipped_quantity
FROM Total_Order_Table tot
LEFT JOIN Order_Detail_Table odt ON odt.Order_Number = tot.Order_Number
GROUP BY tot.Order_no, odt.Order_no, tot.shipped_quantity
(Code not tested in MySQL so forgive errors)

SQL - OutterApply and Left Join

I'm working with a large stored procedure, I'm having trouble with a small portion of it.
When I execute a query on the table im joining, there can be 0, 1 or 2 results. If there are 0 results, I don't really care, my code returns null values, no big deal. If there is 1 result, my code returns the correct values, however, if there are 2 results, I am having trouble selecting the second result.
My code below works until the second OutterApply(the AHM2 stuff). Does anyone see what I am doing wrong?
The animal ID is identical for both OuterApplys. I just need to return the second result, if there is one, and if it is not the same as the first one.
SELECT TOP 1
AHM.AnimalHerdManagementId,
AHM.HerdManagementId,
AHM2.AnimalHerdManagementId,
AHM2.HerdManagementId,
HM.Code AS HerdManagementCode,
HM2.Code AS HerdManagementCode2
OUTER APPLY
(
SELECT TOP 1 AHM.AnimalHerdManagementId, AHM.HerdManagementId
FROM dbo.AnimalHerdManagement AHM
WHERE AHM.AnimalId = A.AnimalId AND ISNULL(AHM.EffectiveFrom, #EffectiveFrom) <= #EffectiveFrom
ORDER BY AHM.EffectiveFrom DESC
) AHM
LEFT JOIN dbo.HerdManagement HM ON AHM.HerdManagementId = HM.HerdManagementId
OUTER APPLY
(
SELECT TOP 1 AHM2.AnimalHerdManagementId, AHM2.HerdManagementId
FROM dbo.AnimalHerdManagement AHM2
WHERE AHM2.AnimalId = A.AnimalId AND AHM2.AnimalHerdManagementId != AHM.AnimalHerdManagementId AND ISNULL(AHM2.EffectiveFrom, #EffectiveFrom) <= #EffectiveFrom
ORDER BY AHM2.EffectiveFrom DESC
) AHM2
LEFT JOIN dbo.HerdManagement HM2 ON AHM2.HerdManagementId = HM2.HerdManagementId
I think I can help you with the OUTER APPLY but the method of getting the two different values is going to need some help as my solution is a total hack.
First, you don't need to join on the outer apply. The join is implied. So you can completely eliminate the join syntax from your query.
Second, AnimalHerdManagement looks/seems like a special table called a Junction Table. All the data contained in it is contained elsewhere (That it contains completely redundant data is why it's called a special table). But that is minor.
Finally, here is some example code I threw together that accomplishes what you are after. The method I am using to retrieve different results on the two outer apply's is a hack, but if you are sure that will always be true, it might work. I am not able to get a multi-level outer apply to work.
select * from AH_Animal A
outer apply
(
select max (HerdManagementID) as HerdMgmtID1 from AH_AnimalHerdManagement HM1 where HM1.AnimalID = A.AnimalID
) as z
outer apply
(
select min (HerdManagementID) as HerdMgmtID2 from AH_AnimalHerdManagement HM2 where HM2.AnimalID = A.AnimalID
) as zz
I hope that helped. There has to be another solution to this, as this would not work at all if you ever expected 3 results.
Query Results:

Select slugs from TABLE_A that are not already in TABLE_B MySQL

I have the following mySQL code written in PHP
SELECT * FROM $photos;
I then have some code which adds the photo slug to a database into table 2. The photos table can grow large, so I want the SELECT above to only SELECT photos where the photo hasn't already been added. There's no timestamp or anything to work from, i.e something like
SELECT * FROM $photos INNER JOIN $slugs WHERE $photos. 'slugs' <> $slugs . 'slugs'
Am I going in the right direction here, INNER join is a bit confusing
Here's how to do it using a LEFT JOIN, which tends to be much faster than the NOT IN:
SELECT * FROM $photos
LEFT JOIN $slugs ON $photos.slugs = $slugs.slugs
WHERE $slugs.slugs IS NULL;
Yes you are going in the right direction but I think you can better use a Left Join instead of using a where clause.
Try like this:-
SELECT * FROM $photos LEFT JOIN $slugs ON $photos.slugs = $slugs.slugs
WHERE $photos.slugs IS NULL;