SQL: Multiple matches towards same table? - mysql

Probably a very basic solution to this, but I can't seem to figure it out. I'm trying to match
multiple ID's in one table towards another.
Structure is something like this,
tt_staff (ID - name)
2 - Lenny
3 - Carl
tt_run (producer1 - producer2)
2 (i.e Lenny)
3 (i.e Carl)
I want to create a view with a single row that shows me the names of both Carl and Lenny, rather then their ID's.
Tried the following,
SELECT e.*, run.*, s.s_name FROM tt_run AS run, tt_events AS e, tt_staff AS s
WHERE e.e_ID = run.e_ID
AND run.e_bandproducer1 = s.s_ID
AND run.e_bandproducer2 = s.s_ID
This obviously doesn't work, since the ID is found on producer1. I've also tried with UNION, but not familiar enough with it (I did manage to get the right result, but in two rows).
As always, thanks for any replies.

It looks like you just need to join tt_staff twice:
SELECT e.*, r.*, s1.s_name, s2.s_name
FROM tt_events e
INNER JOIN tt_run r ON e.e_ID = r.e_ID
INNER JOIN tt_staff s1 ON r.e_bandproducer1 = s1.s_ID
INNER JOIN tt_staff s2 ON r.e_bandproducer2 = s2.s_ID

Related

how do i join third table values into main join?

in query here i have https://www.db-fiddle.com/f/32Kc3QisUEwmSM8EmULpgd/1
SELECT p.prank, d.dare
FROM dares d
INNER JOIN pranks p ON p.id = d.prank_id
WHERE d.condo_id = 1;
i have one condo with id 1 and it have unique connection to dares that has connection to pranks and unique connection to condos_pranks
and i wanna have all unique pranks from both tables and i used this query above to get relation of
dares to pranks and expected result was L,M,N - Yes,No,Maybe and it is correct but i also wanna have those in condos_pranks which ids are 1,4,5,6 = L,O,P,Q
so i tried to join the table with left join because it might not have condos_pranks row
SELECT p.prank, d.dare
FROM dares d
INNER JOIN pranks p ON p.id = d.prank_id
LEFT JOIN condos_pranks pd ON pd.condo_id = d.condo_id AND pd.prank_id = p.id
WHERE d.condo_id = 1;
but result is same as first and what i want is
prank
dare
L
Yes
M
No
N
Maybe
O
No
P
No
Q
No
with default being No = 2 if prank_id of condos_pranks is not in dares
how to connect it?
This seems like an exercise in identifying extraneous information more than anything. You are unable to join something to a table that has no key, however if you know your default then you may use something like coalesce to identify the records where there was no data to join NULL and replace them with your default.
I mentioned in a comment above that this table schema makes little sense. You have keys all over the place that doing have all sorts of circular references. If this is your derived schema, consider stopping here and revisiting the relationships. If it is not and it is something educational, which I suspect it is, disregard and recognize the logical flaws in what you are working in. Perhaps consider taking the data provided and creating a new table schema that is more normalized and uses other tables to handle the many to many and one to many relationships.
dbfiddle
SELECT
pranks.prank,
COALESCE(dares.dare, 'No')
FROM pranks LEFT OUTER JOIN
dares ON pranks.id = dares.prank_id
ORDER BY pranks.prank ASC;
clearlyclueless gave correct explanations
To achieve the result, the following SELECT can also be used:
SELECT
pranks.prank,
case
when dare is null then 'No'
else dare
end
FROM pranks LEFT OUTER JOIN
dares ON pranks.id = dares.prank_id

How do i do a join query in mysql on this scheme

I'm having some trouble trying to get all the data out of this database in one query.
Here is the Database scheme
I know that there is a way to do this with a join but i'm not sure how.
I need all the data from Questionnaire, Booking, and Customers.
I think i have to join on the Booking has customers but i'm not sure.
At the moment i have something like this
SELECT *
FROM Booking,Questionaires,Customers
WHERE accepted = 0
JOIN ON Booking_idBooking = Customers_idCustomers
Can anyone help me out?
Thanks beforehand!
You could use inner join this way
SELECT * FROM Booking_idBooking as BB
INNER JOIN Booking as B on BB.Booking_idBooking = B.idBooking
INNER JOIN Customers as C on BB.Customers_idCustomers = C.idCustomers
INNER JOIN Questionaires as Q on Q.idQuestionaires = B.uestionaires_idQuestionaires
WHERE B.accepted = 0 ;
I would go about doing something like this, but you may need left joins depending one your circumstances.
select
*
from
customers c
inner join booking_has_customers bhs on
bhs.Customers_idCustomers = c.idCustomers
inner join booking b on
b.idBooking = booking_idBooking
inner join Questionaires q on
q.idQuestionaires = b.Questionairs_idQuestionairs
where
b.accepted = 0;
Just a note, you should really change your naming conventions.
For example booking is singular, yet customers is plural in your schema. They should be consistent. Also, something like idBooking and booking_idBooking is not great - maybe use id and booking_id. Anyway just suggestions.
You also might want to invest some time in learning how to write joins. Checkout w3schools for good examples.
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.idQuestionaries=b.Questionaries_idQuestionaries
JOIN Booking_has_Customers AS bhc ON bhc.Booking_idBooking=b.idBooking
JOIN Customers AS c ON c.idCustomers=bhs.Customers_idCustomers
WHERE b.accepted=0
But come on, change ID names into simple id and you'll have:
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.id=b.id
JOIN Booking_has_Customers AS bhc ON bhc.id=b.id
JOIN Customers AS c ON c.id=bhs.id
WHERE b.accepted=0

Joining three tables together based on common ids

What I'm looking to generate is the wp_tylerposts table as it is, with the same specifications as the query (selecting only those with post_type='sponsor', but I'd like to reference the other two tables (object_id from wp_tylerterm_relationships and the corresponding name from wp_tylerterms) so that added on to wp_tylerpostswould be the corresponding name value from wp_tylerterms (added to the table would be "Gold Sponsors", for example).
Hopefully that makes sense. I'm sure there's a pretty simple solution, and I've tried my hand at some join queries without any luck... haven't done any of the stuff in a long time. Any help would be much appreciated!
Edit: I've come closer, I think, but I still can't retrieve the "name" column value from wp_tylerterms, here's what I have:
SELECT
c.ID, c.post_title, a.name
FROM
wp_tylerposts c
LEFT JOIN
wp_tylerterm_relationships b
ON
c.ID = b.object_id
LEFT JOIN
wp_tylerterms a
ON
b.object_id = a.term_id
WHERE
c.post_type = 'sponsor'
In second join, you use b.object_id instead of b.term_taxonomy_id. Your query should look like this:
SELECT
c.ID, c.post_title, a.name
FROM
wp_tylerposts c
LEFT JOIN
wp_tylerterm_relationships b
ON
c.ID = b.object_id
LEFT JOIN
wp_tylerterms a
ON
b.term_taxonomy_id = a.term_id
WHERE
c.post_type = 'sponsor'

Difficult to complete a tough query script for me

SELECT a.acikkapali,
b.onay, b.evrakno,
b.tarih,
a.kod,
d.ad,
a.note0,
a.sf_miktar,
a.sf_sf_unit,
a.rtalepedilentestarih,
c.evrakno
FROM stok47T a
LEFT JOIN stok47e b on a.evrakno = b.evrakno
LEFT JOIN stok46t1 c on a.evrakno = c.talepno
LEFT JOIN stok00 d on a.kod = d.kod
WHERE a.tarih = '2013/04/15'
I need to add two my tables into that script with right way that means If I mapped one of them then the normal row count increases this makes me crazy, I have been trying to solve that issue for a couple days but I had been fail many times.
I couldn't find a good mapped fields between stok47t and the others. But there are still a few columns(fields) matches for their types and data.
I need to listen ppl opinions and learns something.
Here is a big part of my query
If you are getting increase in row count then chances are it could be due to using LEFT JOIN. An INNER join might help (see http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html guidance)
SELECT a.acikkapali,
b.onay, b.evrakno,
b.tarih,
a.kod,
d.ad,
a.note0,
a.sf_miktar,
a.sf_sf_unit,
a.rtalepedilentestarih,
c.evrakno
FROM stok47T a
INNER JOIN stok47e b on a.evrakno = b.evrakno
INNER JOIN stok46t1 c on a.evrakno = c.talepno
INNER JOIN stok00 d on a.kod = d.kod
WHERE a.tarih = '2013/04/15'
However without understanding your data structure then there is a chance you might lose the information that you are after.
If you are getting multiple rows, it is probably due to a Cartesian product occurring in the joins. This is unrelated to the type of join (left/right/full/inner). It is based on the relationships between the tables. You have 1-N relationships along different dimensions.
Your conditions are:
FROM stok47T a
LEFT JOIN stok47e b on a.evrakno = b.evrakno
LEFT JOIN stok46t1 c on a.evrakno = c.talepno
LEFT JOIN stok00 d on a.kod = d.kod
I have no idea what these tables and fields mean. But, if you have a case where there is one row per evrakno in table stok47t, and there are two rows in table stok47e and three in table stok46t1, then you will get six rows in the output.
Without more information, it is impossible to tell you the best solution. One method is to summarize the tables. Another is to take the first or last corresponding row, by doing something like:
from stok47T a left join
(select s.*, row_number() over (partition by evrakno order by id desc) as seqnum
from stok47e s
) b
on a.evrakno = b.evrakno and b.seqnum = 1

Getting Repeated values in SQL

I was desperately trying harder and harder to get this thing done but didn`t yet succeed. I am getting repeated values when i run this query.
select
tbl_ShipmentStatus.ShipmentID
,Tbl_Contract.ContractID,
Tbl_Contract.KeyWinCountNumber,
Tbl_Item.ItemName,
Tbl_CountryFrom.CountryFromName,
Tbl_CountryTo.CountryToName,
Tbl_Brand.BrandName,
Tbl_Count.CountName,
Tbl_Seller.SellerName,
Tbl_Buyer.BuyerName,
Tbl_Contract.ContractNumber,
Tbl_Contract.ContractDate,
tbl_CountDetail.TotalQty,
tbl_CostUnit.CostUnitName,
tbl_Comission.Payment,
tbl_Port.PortName,
Tbl_Contract.Vans,
tbl_Comission.ComissionPay,
tbl_Comission.ComissionRcv,
tbl_CountDetail.UnitPrice,
tbl_Comission.ComissionRemarks,
tbl_CountDetail.Amount,
tbl_LCStatus.LCNumber,
tbl_ShipmentStatus.InvoiceNumber,
tbl_ShipmentStatus.InvoiceDate,
tbl_ShipmentStatus.BLNumber,
tbl_ShipmentStatus.BLDate,
tbl_ShipmentStatus.VesselName,
tbl_ShipmentStatus.DueDate
from tbl_ShipmentStatus
inner join tbl_LCStatus
on
tbl_LCStatus.LCID = tbl_ShipmentStatus.LCStatusID
inner join Tbl_Contract
on
tbl_LCStatus.ContractID = Tbl_Contract.ContractID
inner join Tbl_CountDetail
on Tbl_Contract.ContractID = Tbl_CountDetail.ContractId
inner join tbl_Comission
on
tbl_Comission.ContractID = Tbl_Contract.ContractID
inner join Tbl_Item
on
Tbl_Item.ItemID = Tbl_Contract.ItemID
inner join Tbl_Brand
on Tbl_Brand.BrandID = Tbl_Contract.BrandID
inner join Tbl_Buyer
on Tbl_Buyer.BuyerID = Tbl_Contract.BuyerID
inner join Tbl_Seller
on Tbl_Seller.SellerID = Tbl_Contract.SellerID
inner join Tbl_CountryFrom
on Tbl_CountryFrom.CountryFromID = Tbl_Contract.CountryFromID
inner join Tbl_CountryTo
on
Tbl_CountryTo.CountryToID = Tbl_Contract.CountryToID
inner join Tbl_Count
on
Tbl_Count.CountID = Tbl_CountDetail.CountId
inner join tbl_CostUnit
on tbl_Comission.CostUnitID = tbl_CostUnit.CostUnitID
inner join tbl_Port
on tbl_Port.PortID = tbl_Comission.PortID
where tbl_LCStatus.isDeleted = 0
and tbl_ShipmentStatus.isDeleted =0
and tbl_LCStatus.isDeleted = 0
and Tbl_CountDetail.isDeleted = 0
and Tbl_Contract.isDeleted = 0
and tbl_ShipmentStatus.LCStatusID = 5
I have also attached a picture of my result set of rows.
Any suggestions why this is happening would really be appreciable.
Result Set
Typically this happens when you have an implicit partial cross join (Cartesian product) between two of your tables. That's what it looks like to me here.
This happens most often when you have a many-to-many relationship. For example, if a single Album allows both multiple Artists and multiple Songs and the only relationship between Artists and Songs is Album, then there's essentially a many-to-many relationship between Artists and Songs. If you select from all three tables at once you're going to implicitly cross join Artists and Songs, and this may not be what you want.
Looking at your query, I see many-to-many between Tbl_CountDetail and tbl_Comission through Tbl_Contract. Try eliminating one of those joins to test to see if the behavior disappears.
Try using the DISTINCT keyword. It should solve your issue
Select DISTINCT ....
Wait as far as I can see your records are not duplicates.
HOWEVER
Notice the CountName column and Shipment ID column
The combination is unique for every row. Hence the values are unique as far as I can see. Try not selecting CountName.
Well if you have distinct rows its not a duplication problem. The issue is during the join a combination is occurring you don't want it to duplicating the results.
Either don't select CountName or you have a mistake in your data.
Only one of those rows should be true either 6 with Count2 or 6 with Count1. Likewise for 7. The fact that your getting both when your not supposed to indicates a logic mistake