I'm struggling with creating my SQL Query for some Wordpress posts.
There are 4 Tables:
posts
term_relationships
term_taxonomy
terms
What I want to achieve:
post_title
rhythmus
taktart
stilrichtung
urheber
harmonika_rallye
Polka
2/4-Takt
Oberkrainer
Manfred Eisl
I was trying around a lot now with the PIVOT function but I cannot get it. That's what I have so far.
SELECT
*
FROM wNfVMposts p
LEFT JOIN wNfVMterm_relationships tr
ON p.ID=tr.object_id
LEFT JOIN wNfVMterm_taxonomy tt
ON tr.term_taxonomy_id=tt.term_taxonomy_id
LEFT JOIN wNfVMterms t
ON tt.term_id=t.term_id
WHERE p.ID = '1495'
I'm getting 4 Results at the moment. Would appreciate any help 🙌
Related
I want to make a query in which if the query does not find records for the query not exist, then it looks to see if these codes are present in the array field. The join will go through the code c_oplmlp/c_serv
select distinct ocf.code_head_mo , cs.c_oplmp, r."name", ocf.code_state, rm2.name_short,
ocf.id_condition_mc
from puomp.op_registry r
join puomp.op_case_finish ocf on r.id_registry =ocf.id_registry
join puomp.op_patient pt on ocf.id_patient =pt.id_patient
join puomp.op_case sl on ocf.id_case_finish =sl.id_case_finish
join puomp.op_case_ext cs on sl.id_case =cs.id_case
left join nsi.ref_mo rm2 on r.id_head_mo =rm2.id_mo
where not exists(select * from lic_app_new lm
left join ksg_p kp
where
lm.fc_mo=ocf.code_head_mo
and (lm.c_serv =cs.c_oplmp) if( (lm.c_serv !=cs.c_oplmp) then
lm.c_serv=kp.smj_prof )
endif;
)
ksg_p table look like this
ksg_p
|---id
|---name
|---c_prof(same as c_serv)(main code)
|---smj_prof(related to c_prof)
The idea is if query does not find by main code(c_serv) it search by related codes. This is codes of illneses. Smj_prof contains related codes of illness code. For example ucler has a code of 3( witch is code of gastroenterology) and it has related codes of 9(therapy) and 10(pediatrics). It means that pacient can be treated in gastroenterology, therapy and pediatrics
Help pls i dont even had an idea of how to do this.
What if you need just add public. to SELECT options
example:
select distinct public.ocf.code_head_mo, public.cs.c_oplmp, ...etc...
I am trying to join 4 tables together as shown in this diagram here:
https://imgur.com/a/jukJvSw
The SQL query I have written returns all fields except TExpiryDate and I have not come across any examples online that can help me understand this. Please help.
SELECT tbPurchaseHeader.PurchaseDate,
tbSupplier.CompanyName,
tbPurchaseDetails.UnitCost,
tbPurchaseDetails.Quantity,
tbPurchaseDetails.Bonus,
tbpurchasedetails.BatchID,
tbBatch.TExpiryDate
FROM ((tbPurchaseDetails
INNER JOIN tbPurchaseHeader
ON tbPurchaseDetails.PurchaseID = tbPurchaseHeader.PurchaseID)
LEFT JOIN tbBatch
ON tbPurchaseDetails.BID = tbBatch.BID)
INNER JOIN tbSupplier
ON tbPurchaseHeader.SupplierID = tbSupplier.SupplierID
WHERE tbPurchaseDetails.ProductID = ?
ORDER BY tbPurchaseHeader.PurchaseDate
Turns out BID contains empty values in the database. I have decided to make links elsewhere to get the data. Thanks everyone for making me realise this.
I made a query and wanted to not have any duplicates but i got some times 3 duplicates and when i used DISTINCT or DISTINCTROW i got only 2 duplicates.
SELECT f.flight_code,
f.status,
a.airport_name,
a1.airport_name,
f.departing_date+f.departing_time AS SupposedDepartingTime,
f.landing_date+f.landing_time AS SupposedLandingTime,
de.actual_takeoff_date+de.actual_takeoff_time AS ActualDepartingTime,
SupposedLandingTime+(ActualDepartingTime-SupposedDepartingTime) AS ActualLandingTime
FROM
(((Flights AS f
LEFT JOIN Aireports AS a
ON a.airport_code = f.depart_ap)
LEFT JOIN Aireports AS a1
ON f.target_ap = a1.airport_code)
LEFT JOIN Irregular_Events AS ie
ON f.flight_code = ie.flight_code)
LEFT JOIN Delay_Event AS de
ON ie.IE_code = de.delay_code;
had to use LEFT JOIN because when i used INNER JOIN i missed some of the things i wanted to show because i wanted to see all the flights and not only the flights that got delayed or canceled.
This is the results when i used INNER JOIN, you can see only the flights that have the status "ביטול" or "עיכוב" and that is not what i wanted.
[the results with LEFT JOIN][2]
[2]: https://i.stack.imgur.com/cgE2G.png
and when i used DISTINCT where you see the rows with the NUMBER 6 on the first column it appear only two times
IMPORTANT!
I just checked my query and all the tables i use there and i saw my problem but dont know how to fix it!
in the table Irregular_Events i have more the one event for flights 3,6 and 8 and that is why when i use LEFT JOIN i see more even thou i use distinct, please give me some help!
Not entirely sure without seeing the table structure, but this might work:
SELECT f.flight_code,
f.status,
a.airport_name,
a1.airport_name,
f.departing_date+f.departing_time AS SupposedDepartingTime,
f.landing_date+f.landing_time AS SupposedLandingTime,
de.actual_takeoff_date+de.actual_takeoff_time AS ActualDepartingTime,
SupposedLandingTime+(ActualDepartingTime-SupposedDepartingTime) AS ActualLandingTime
FROM
((Flights AS f
LEFT JOIN Aireports AS a
ON a.airport_code = f.depart_ap)
LEFT JOIN Aireports AS a1
ON f.target_ap = a1.airport_code)
LEFT JOIN
(
SELECT
ie.flight_code,
de1.actual_takeoff_date,
de1.actual_takeoff_time
FROM
Irregular_Events ie
INNER JOIN Event AS de1
ON ie.IE_code = de1.delay_code
) AS de
ON f.flight_code = de.flight_code
It is hard to tell what is the problem with your query without any sample of the output, and without any description of the structure of your tables.
But your problem is that your are querying from the flights table, which [I assume] can be linked to multiple irregular_events, which can possibly also be linked to multiple delay_event.
If you want to get only one row per flight, you need to make sure your joins return only one row too. Maybe you can do it by adding one more condition to the join, or by adding a condition in a sub-query.
EDIT
You could try to add a GROUP BY to the query:
GROUP BY
f.flight_code,
f.status,
a.airport_name,
a1.airport_name;
I have three tables that I'm trying to join to get the necessary data. Here they are...
*TblComp* *TblCompParent* *tblCompProcesses*
CompID CompBillingID CompID
CompBillingID Capacity1 CompProcessID
Capacity2
So what I'm trying to do with these three tables is....
Select tblCompParent.Capacity1, tblCompParent.Capacity2, CompProcessID
My problem is this...In tblComp there are 351 values - so i start off by joining tblComp and tblCompparent and SELECTING tblCompparent.capacity1, tblcompparent.capacity2, the query looks like this...
SELECT dbo.tblComp.CompID, dbo.tblCompParents.Capacity1, dbo.tblCompParents.Capacity2
FROM dbo.tblCompParents INNER JOIN
dbo.tblComp ON dbo.tblCompParents.CompBillingID = dbo.tblComp.CompBillingID
And this works fine, it's when I try to join tblCompProcess to pull the CompProcessID is when I get like 580 records. I'm not sure what can of join I have to do on tblCompprocess to select only one CompProcessID per compID.
And it seems like i have to use tblComp otherwise I'll have no way of joining tblCompProcess.
EDIT1:
SELECT dbo.tblComp.CompID, dbo.tblCompParents.Capacity1, dbo.tblCompParents.Capacity2, tblCompProcess.compprocessID
FROM dbo.tblCompParents INNER JOIN
dbo.tblComp ON dbo.tblCompParents.CompBillingID = dbo.tblComp.CompBillingID
Inner Join dbo.tblCompprocess on tblCompProcess.CompID = tblComp.CompID
You don't show us the whole data model so I don't know exactly what is going on but clearly tblCompProcesses has more than one row in your join. I would fix it like this:
SELECT dbo.tblComp.CompID, dbo.tblCompParents.Capacity1, dbo.tblCompParents.Capacity2, x.compprocessID
FROM dbo.tblCompParents
INNER JOIN dbo.tblComp ON dbo.tblCompParents.CompBillingID = dbo.tblComp.CompBillingID
INNER JOIN (SELECT DISTINCT CompID, compprocessID
FROM dbo.tblCompprocess) X on x.CompID = tblComp.CompID
Having this problem, I don `t know how to pull data from multiple tables. I tried to use INNER JOIN and UNION.
Here is what I came so far:
select `name_ganre` from `teleprogram`.`ganres`
where `idganre`=any(select `idganre` from `teleprogram`.`ganre-transfer`
where `idtransfer`=any(select `idtransfer` from `teleprogram`.`broadcasting`));
select `name_channel` from `teleprogram`.`channel`
where `idchannel`= any(select `idchannel` from `teleprogram`.`broadcasting`);
I need to bring in one column name of the channel. And another name for the genre.
broadcasting
rows: idtransfer, idchannel
transfer
rows: idtransfer, name_transfer
ganre-transfer
rows: idganre, idtransfer
ganre
rows: idganre, name_ganre
channels
rows: idchannel, name_channel
I'm trying to get the data through broadcasting. May be simplified?
After the query: name_channel, name_ganre
Thank you!
The query that you need is this:
select c.name_channel, g.name_ganre
from channels c
inner join broadcasting b on b.idchannel = c.idchannel
inner join ganre-transfer gt on gt.idtransfer = b.idtransfer
inner join ganre g on g.idganre = gt.idganre
You can see how INNER JOIN works HERE!