This is the table structure
plinks
link
ojectidfk
uidfk
ojects
ojectid
uidfk
ojectname
Try
useridfk
ojectidfk
goryidfk
gory
goryid
goryname
What i want to do is select the ojectname from ojects where the plinks projectidfk is the same in ojects and plinks but select everything from try where the ojectid is equal to the ojectsid where pinks link = 8493284 AND gory id = try goryidfk
Select obj.objectnamem, try.* from objects as obj
inner join plinks on plinks.projectidfk = obj.ojectid
inner join try on try.projectidfk = obj.ojectid
inner join gory on gory.goryid = try.goryidfk
where plinks.link = 8493284
Try joining all four tables and use where clause for link like:
SELECT o.objectName, t.*
FROM ojects o INNER JOIN plinks p ON o.ojectId = p.ojectidfk
INNER JOIN try t ON o.ojectid = t.ojectidfk
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE p.link = 8493284
Using simple JOIN should do this for you. You just connect tables by constraints and then retrieve whatever you want from any of those tables.
Please notice, that if you write a pseudo-code it would look pretty similar to the actual code. You need to use INNER JOINs because you want to be sure, that there are rows that share all those relations and match your criteria on plinks.link.
SELECT o.ojectname, t.*
FROM
ojects o
INNER JOIN plinks p ON p.ojectidfk = o.ojectid
INNER JOIN try t ON t.ojectidfk = o.ojectid
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE
p.link = 8493284
Related
Hello i'm learning sql and i have some issues with joins(which i have problems understanding them)
I have this issue
#1066 - Not unique table/alias: 'tbl_respuestas'
what the query supposed to do, is count how many people(general,ignore user) has answer 'x', in 'y' question of 'z' survey
SELECT COUNT(*) FROM tbl_respuestas
INNER JOIN tbl_encuesta_usuario ON tbl_encuesta_usuario.user_id = user.id
INNER JOIN tbl_encuesta ON tbl_encuesta.id = tbl_encuesta_usuario.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas ON tbl_encuesta_has_tbl_preguntas.tbl_encuesta_id = tbl_encuesta.id
INNER JOIN tbl_preguntas ON tbl_preguntas.id = tbl_encuesta_has_tbl_preguntas.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas ON tbl_preguntas_has_tbl_respuestas.tbl_preguntas_id = tbl_preguntas.id
INNER JOIN tbl_respuestas ON tbl_respuestas.id = tbl_preguntas_has_tbl_respuestas.tbl_respuestas_id
WHERE tbl_respuestas.respuesta = 2
line SELECT COUNT(*) FROM tbl_respuestas
and line INNER JOIN tbl_respuestas
does not makes sense, hence the error.
Unless it is what you want then you need to give then different name/alias like below:
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_respuestas r2
Also as a quick note you can rewrite the entire sql like below.
It is good practice to give your tables a name for shorter referencing and makes the sql look a little cleaner.
Also if both tables you are trying to join has the same column name then you can use the keyword USING instead of having to write that long line tbl_encuesta_usuario.user_id = user.id
Please be sure to put r and r2 in its prope place
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_encuesta_usuario u USING user_id
INNER JOIN tbl_encuesta e ON e.id = u.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas hp ON hp.tbl_encuesta_id = e.id
INNER JOIN tbl_preguntas p ON p.id = hp.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas hr ON hr.tbl_preguntas_id = p.id
INNER JOIN tbl_respuestas r2 ON r2.id = hr.tbl_respuestas_id
WHERE r.respuesta = 2
I want to use SUM function in alias
please look at following code i hope you understand it:
SELECT PATIENTS.NAME,
PATIENTS.FAMILY,
COST.COST,
COST.REALDATEYEAR,
COST.REALDATEMONTH,
COST.REALDATEDAY,
CONTRACTS.ID,
CONTRACTS.FRANSHIZ AS CO1,
PATIENTS.COMPANY,
CONTRACTS.COMPANY,
COST.TYPE,
TARRIFS.ID,
TARRIFS.ACTION,
TARRIFS.COST AS CO
FROM PATIENTS
INNER JOIN COST
INNER JOIN CONTRACTS
INNER JOIN TARRIFS
ON PATIENTS.SINGLE_ID = COST.SINGLE_ID
AND COST.TYPE = TARRIFS.ID
AND CONTRACTS.ID = PATIENTS.COMPANY
i want to use SUM for co1 and co,
You need to use GROUP BY when using SUM.
Your query something like this.
SELECT PATIENTS.NAME,
PATIENTS.FAMILY,
COST.COST,
COST.REALDATEYEAR,
COST.REALDATEMONTH,
COST.REALDATEDAY,
CONTRACTS.ID,
SUM(CONTRACTS.FRANSHIZ) AS CO1,
PATIENTS.COMPANY,
CONTRACTS.COMPANY,
COST.TYPE,
TARRIFS.ID,
TARRIFS.ACTION,
TARRIFS.COST AS CO
FROM PATIENTS
INNER JOIN COST
INNER JOIN CONTRACTS
INNER JOIN TARRIFS
ON PATIENTS.SINGLE_ID = COST.SINGLE_ID
AND COST.TYPE = TARRIFS.ID
AND CONTRACTS.ID = PATIENTS.COMPANY
GROUP BY CONTRACTS.FRANSHIZ
I'm working on a project that each orgnizations have barriers, each organization can contain 1 or more barriers, and then I need generate an XML file that show each barrier into the organization .
But executing this sql code:
SELECT organizations.*, barriers.barrierName AS bname, barriers.type AS btype, geographs.name AS geo, rpd.rpdname AS rpdn, rpd.meaning AS rpdmean FROM organizations
left join orgbarriers on orgbarriers.idOrg = organizations.id
left join barriers on orgbarriers.idBarrier = barriers.id
left join orggeographs on organizations.id = orggeographs.idOrg
left join geographs on geographs.id = orggeographs.idGeo
left join orgrpds on orgrpds.idOrg = organizations.id
left join rpd on rpd.id = orgrpds.idRPD
I get repeated rows like this image:
Use the keyword "DISTINCT"
SELECT DISTINCT rest of your query
Now how i select the project_title from table2 having tm_id=10
and what is the best way to do this task?
SELECT t2.project_title FROM table2 AS t2
JOIN table3 AS t3 ON t3.project_id = t2.project_id
WHERE t3.tm_id = 10;
I think a simple INNER JOIN will suffice your need.
SELECT a.*, c.project_title
FROM Online_team a
INNER JOIN team_project b
ON a.tm_id = b.tm_id
INNER JOIN online_team_projects c
ON b.project_ID = c.project_ID
WHERE a.tm_id = 10
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
But if you don't need any columns from Online_team, you can remove it from the joins list.
SELECT c.project_title
FROM team_project b
INNER JOIN online_team_projects c
ON b.project_ID = c.project_ID
WHERE b.tm_id = 10
use the query as below
SELECT table2.project_title from table2,table3 where table2.project_id = table3.project_id and table3.tm_id = 10
SELECT
otp.project_title
FROM online_team_projects otp,
team_project tp
WHERE otp.project_id = tp.project_id
AND tp.tm_id = 10
You could use this approach:
SELECT otp.project_title
FROM online_team ot
INNER JOIN online_team_projects otp USING (project_id)
WHERE ot.tm_id = 10
I have Doctrine2 DQL query but I want to build it with QueryBuilder, I have noticed that produced DQL is somewhat different from the handcrafted one, and I'm wondering what am I missing here - maybe I'm not aware of something or doing things wrong way?
Ok, some details:
My handcrafted query looks like this:
select count(fi.id)
from Entities\Content\FolderLookup fl
join fl.site fls
join fl.folder flf,
Entities\Content\FolderItem fi
join fi.site fis
join fi.folder fif
join fi.item it
join it.type tp
join it.content ic
where fl.namePath = ?1
and tp.name = ?2
and fls.id = fis.id
and flf.id = fif.id
Now, I'm trying to reproduce it like this with QueryBuilder:
$qb->select("count(fi.id)")->from("Entities\Content\FolderLookup", "fl")->join("fl.site","fls")->join("fl.folder", "flf");
$qb->from("Entities\Content\FolderItem","fi")->join("fi.site","fis")->join("fi.folder","fif");
$qb->join("fi.item","it")->join("it.type","tp")->join("it.content","ic");
$wherePart = $qb->expr()->andx();
$wherePart->add($qb->expr()->eq("fl.namePath","?1"));
$wherePart->add($qb->expr()->eq("tp.name","?2"));
$wherePart->add($qb->expr()->eq("fls.id","fis.id"));
$wherePart->add($qb->expr()->eq("flf.id","fif.id"));
$qb->where($wherePart);
This however is producing this DQL query:
SELECT count(fi.id) FROM Entities\Content\FolderLookup fl,
Entities\Content\FolderItem fi
INNER JOIN fl.site fls
INNER JOIN fl.folder flf
INNER JOIN fi.site fis
INNER JOIN fi.folder fif
INNER JOIN fi.item it
INNER JOIN it.type tp
INNER JOIN it.content ic
WHERE (fl.namePath = ?1)
AND (tp.name = ?2)
AND (fls.id = fis.id)
AND (flf.id = fif.id)
As you can see there is part of this missing comapring to handcrafted one (First line):
fl join fl.site fls join fl.folder flf
I'm not sure why these joins are missing as I am defining them here:
$qb->select("count(fi.id)")->from("Entities\Content\FolderLookup", "fl")->join("fl.site","fls")->join("fl.folder", "flf");
Update:
The fun part starts, when DQL gets translated into SQL - in this case MySQL:
Handcrafted one becomes:
SELECT count(f0_.id) AS sclr0 FROM FolderLookup f1_ INNER JOIN Site s2_ ON f1_.site_id = s2_.id INNER JOIN Folder f3_ ON f1_.folder_id = f3_.id, FolderItem f0_ INNER JOIN Site s4_ ON f0_.site_id = s4_.id INNER JOIN Folder f5_ ON f0_.folder_id = f5_.id INNER JOIN Item i6_ ON f0_.item_id = i6_.id INNER JOIN ItemType i7_ ON i6_.type_id = i7_.id INNER JOIN ItemContent i8_ ON i6_.content_id = i8_.id WHERE f1_.namePath = ? AND i7_.name = ? AND s2_.id = s4_.id AND f3_.id = f5_.id
Where generated one looks like this:
SELECT count(f0_.id) AS sclr0 FROM FolderLookup f1_, FolderItem f0_ INNER JOIN Site s2_ ON f1_.site_id = s2_.id INNER JOIN Folder f3_ ON f1_.folder_id = f3_.id INNER JOIN Site s4_ ON f0_.site_id = s4_.id INNER JOIN Folder f5_ ON f0_.folder_id = f5_.id INNER JOIN Item i6_ ON f0_.item_id = i6_.id INNER JOIN ItemType i7_ ON i6_.type_id = i7_.id INNER JOIN ItemContent i8_ ON i6_.content_id = i8_.id WHERE (f1_.namePath = ?) AND (i7_.name = ?) AND (s2_.id = s4_.id) AND (f3_.id = f5_.id)
And this is invalid statement, as database returns with:
Column not found: 1054 Unknown column 'f1_.site_id' in 'on clause'
Any ideas welcome.
It seems the DQL parser is wrongly positioning the joins to the wrong from.
My initial suggestion is to try to make only 1 FROM item and a subselect.
Also, I'd love if you add the same content you asked here in our bug tracking: http://www.doctrine-project.org/jira/browse/DDC
Thanks a lot!
Guilherme Blanco
Doctirne Core Developer
they are not missing. just reordered
INNER JOIN fl.site fls
INNER JOIN fl.folder flf