Mysql - associative table - Unknown column in on clause - php

The following query always returns #1054 - Unknown column 'T_RELATIF_SESS.REL_COM_ID' in 'on clause'
SELECT
`T_FORMATIONS`.`FOR_ID`,
`T_FORMATIONS`.`FOR_TITRE`,
`T_FORMATIONS`.`FOR_NIVEAU`,
`T_FORMATIONS`.`FOR_MAX_PART`,
`T_RELATIF_SESS`.`REL_COM_ID`,
`T_RELATIF_SESS`.`REL_SES_ID`,
`T_SESSIONS`.`SES_ID`,
`T_SESSIONS`.`SES_TITRE`,
`T_SESSIONS`.`SES_TYPE`,
`T_SESSIONS`.`SES_ADRESSE`,
`T_SESSIONS`.`SES_NPA`,
`T_SESSIONS`.`SES_LIEU`,
`T_SESSIONS`.`SES_PRIX_SPECIAL`,
`T_SESSIONS`.`SES_VAL_PRIX_SPECIAL`,
`T_SESSIONS`.`SES_PRIX_SPEC_EXP`,
`T_SESSIONS`.`SES_SUPP_COURS_INCL`,
`T_SESSIONS`.`SES_SUPP_COURS_ADD`,
`T_SESSIONS`.`SES_PRIX_SUPP_COURS_ADD`,
`T_SESSIONS`.`SES_HORAIRE_SPECIAL`,
`T_SESSIONS`.`SES_REMARQUES`,
`T_SESSIONS`.`SES_REC_ID`,
`T_COURS`.`COU_ID`,
`T_COURS`.`COU_DATE`,
(SELECT `COU_DATE` FROM `T_COURS` WHERE `T_COURS`.`COU_SES_ID` = `T_SESSIONS`.`SES_ID` ORDER BY `COU_DATE` ASC LIMIT 1) AS `PREMIERCOURS`,
(SELECT `COU_DATE` FROM `T_COURS` WHERE `T_COURS`.`COU_SES_ID` = `T_SESSIONS`.`SES_ID` ORDER BY `COU_DATE` DESC LIMIT 1) AS `DERNIERCOURS`
FROM `T_COURS`
INNER JOIN `T_COMMANDES` ON `T_RELATIF_SESS`.`REL_COM_ID` = `T_COMMANDES`.`COM_ID`
INNER JOIN `T_RELATIF_SESS` ON `T_SESSIONS`.`SES_ID` = `T_RELATIF_SESS`.`REL_SES_ID`
INNER JOIN `T_SESSIONS` ON `T_COURS`.`COU_SES_ID` = `T_SESSIONS`.`SES_ID`
INNER JOIN `T_FORMATIONS` ON `T_SESSIONS`.`SES_FOR_ID` = `T_FORMATIONS`.`FOR_ID`
WHERE `T_COMMANDES`.`COM_ID`=19
This query worked fine before I add
INNER JOIN `T_COMMANDES` ON `T_RELATIF_SESS`.`REL_COM_ID` = `T_COMMANDES`.`COM_ID`
INNER JOIN `T_RELATIF_SESS` ON `T_SESSIONS`.`SES_ID` = `T_RELATIF_SESS`.`REL_SES_ID`
And
WHERE `T_COMMANDES`.`COM_ID`=19
T_RELATIF_SESS is juste an associative table between the tables called "T_SESSIONS" and "T_COMMANDES".
I've tried to rewrite the query many times but I still get this error and I really don't understand why. Every field exists in my database.
I know it may be simple but it really gives me headache. Can someone can give me a hand or just explain to me what I do wrong? It would be much apprecietaded!
Thank you very much!

So from your description you try to run the script without
INNER JOIN `T_COMMANDES` ON `T_RELATIF_SESS`.`REL_COM_ID` = `T_COMMANDES`.`COM_ID`
INNER JOIN `T_RELATIF_SESS` ON `T_SESSIONS`.`SES_ID` = `T_RELATIF_SESS`.`REL_SES_ID`
and it is working, but not with that script? Try to make it in order, since on your first inner join, you are trying to join the table (T_COMMANDES) with the table that you will join next (T_RELATIF_SESS).
So try:
INNER JOIN `T_SESSIONS` ON `T_COURS`.`COU_SES_ID` = `T_SESSIONS`.`SES_ID`
INNER JOIN `T_FORMATIONS` ON `T_SESSIONS`.`SES_FOR_ID` = `T_FORMATIONS`.`FOR_ID`
INNER JOIN `T_RELATIF_SESS` ON `T_SESSIONS`.`SES_ID` = `T_RELATIF_SESS`.`REL_SES_ID`
INNER JOIN `T_COMMANDES` ON `T_RELATIF_SESS`.`REL_COM_ID` = `T_COMMANDES`.`COM_ID`

your order of table are messed up after you add your join, since you are using INNER JOIN, you can declare the table anywhere you like as long as the fields are visible on the joins. It will still yield the same result.
To solve the problem, put those new join on the lower part of your joins:
FROM T_COURS
INNER JOIN T_SESSIONS
ON T_COURS.COU_SES_ID = T_SESSIONS.SES_ID
INNER JOIN T_FORMATIONS
ON T_SESSIONS.SES_FOR_ID = T_FORMATIONS.FOR_ID
INNER JOIN T_RELATIF_SESS
ON T_SESSIONS.SES_ID = T_RELATIF_SESS.REL_SES_ID
INNER JOIN T_COMMANDES
ON T_RELATIF_SESS.REL_COM_ID = T_COMMANDES.COM_ID
The reason why an error message: Unknown column 'T_RELATIF_SESS.REL_COM_ID' in 'on clause' was thrown is because when joining T_COURS with T_COMMANDES, column T_RELATIF_SESS.REL_COM_ID isn't visible yet since you have declared table T_RELATIF_SESS on the lower part of the join.

You try to access T_RELATIF_SESS before you have joined the table. You must join T_COMMANDES after T_RELATIF_SESS and T_RELATIF_SESS after T_SESSIONS
SELECT
`T_FORMATIONS`.`FOR_ID`,
...
FROM `T_COURS`
INNER JOIN `T_SESSIONS` ON `T_COURS`.`COU_SES_ID` = `T_SESSIONS`.`SES_ID`
INNER JOIN `T_FORMATIONS` ON `T_SESSIONS`.`SES_FOR_ID` = `T_FORMATIONS`.`FOR_ID`
INNER JOIN `T_RELATIF_SESS` ON `T_SESSIONS`.`SES_ID` = `T_RELATIF_SESS`.`REL_SES_ID`
INNER JOIN `T_COMMANDES` ON `T_RELATIF_SESS`.`REL_COM_ID` = `T_COMMANDES`.`COM_ID`
WHERE `T_COMMANDES`.`COM_ID`=19

Related

INNER JOIN with multiple tabels

I Have PHP Script That SELECT FROM 7 Tabels and client_id is the Common Column
And Return Single Tebel With all the clients by as there client_id row after row, now The problem is the if there is in one Tabel tow rows with 2 duplicate id That's print duplicate rows like 100 times
maybe i don't do INNER JOIN right or something if someone has an idea how to prevent this
$stmt = $pdo->query("SELECT * FROM client_form cf
INNER JOIN client_form_2 cf2 ON cf.client_id = cf2.client_id
INNER JOIN client_form_3 cf3 ON cf.client_id = cf3.client_id
INNER JOIN client_form_4 cf4 ON cf.client_id = cf4.client_id
INNER JOIN client_form_5 cf5 ON cf.client_id = cf5.client_id
INNER JOIN client_form_6 cf6 ON cf.client_id = cf6.client_id
INNER JOIN client_form_7 cf7 ON cf.client_id = cf7.client_id
");

retrieving values from multiple tables in mysql

I am using php and I have to get the data from multiple tables with common id, but the problem is that in few tables that common id contains multiple records,using inner join gives me separate rows of data e.g.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"2"}
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"3"}
extraId is the multiple record for the dish_id:52.
I need result like this.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId"[{"id":"2"},{"id":"3"]}}
My query is:
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status'";
please help.
Use group by and group_concat. Something like this:
Select d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type,
group_concat(e.extra_id) as extraids
from order_main m INNER JOIN
order_detail d
ON m.id = d.order_id INNER JOIN
order_extras e
ON m.id = e.order_id INNER JOIN
order_addons a
ON m.id = a.order_id
where m.franchise_id = '$storeId' and d.STATUS <> '$order_status'
group by d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type;
Your desired results do not include these columns:
e.extra_title
a.addon_id
a.addon_size
I would also suggest that you remove the join to order_addons.
Notice that table aliases make the query easier to write and to read.
You can use group bywith dish_id
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status' group by order_detail.dish_id";

MySQL join multiple tables error

I struggle to use join on multiple tables. When I try to do this:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`
I get this:
Unknown column 'absences.employee_FK' in 'on clause'
'absences.employee_FK' exists in my DB.
I want to display the user data and the type of the absence. How can I do that? I dont understand joins too well yet.
Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:
SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
If you want to join to the type table too:
SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
You have to select all the tables for using the JOIN condition.
The example goes like this:
SELECT `employee.*`, `absences.*`, `type.*`
FROM `employee`
JOIN `absences`
ON `employee`.`employee_ID` = `absences`.`employee_FK`
JOIN `type`
ON `absences`.`type_FK` = `type`.`type_ID`
JOIN `on_off`
ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;
You can modify the query as per your requirement.
You can work on the script below. Add Where clause at the end if necessary. Not tested...
SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)
This might be what you are looking for
select * from `absences` a
left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
left outer join `type` t on t.`type_ID`=a.`type_FK`
left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`
You have to use join for all tables:
SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID`

MySQL inner join two columns on one column

I'm stuck on the inner join scenario for my tables. Can you please help me? What I am doing wrong here?
Here is the scenario that I'm trying to do:
I have billing and shipping country/states columns which I am trying to populate with inner join, but somehow it is not working. Thanks a lot for your help:
$sql = $this->connection->query("SELECT * FROM ".TBL_USERS."
INNER JOIN ".TBL_USER_LEVEL." ON ".TBL_USERS.".userlevel = ".TBL_USER_LEVEL.".user_level_id
INNER JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".country OR ".TBL_USERS.".bcountry = ".TBL_COUNTRIES.".country_id
INNER JOIN ".TBL_STATES." ON ".TBL_USERS.".states OR ".TBL_USERS.".bstates = ".TBL_STATES.".states_id
WHERE ".TBL_USERS.".username = '$username'");
i changed the query to; and the error im gettin is
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'countries'' in
$sql = $this->connection->query("SELECT * FROM ".TBL_USERS."
INNER JOIN ".TBL_USER_LEVEL." ON ".TBL_USERS.".userlevel = ".TBL_USER_LEVEL.".user_level_id
INNER JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".country = ".TBL_COUNTRIES.".country_id
INNER JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".bcountry = ".TBL_COUNTRIES.".country_id
INNER JOIN ".TBL_STATES." ON ".TBL_USERS.".states = ".TBL_STATES.".states_id
INNER JOIN ".TBL_STATES." ON ".TBL_USERS.".bstates = ".TBL_STATES.".states_id
WHERE ".TBL_USERS.".username = '$username'");
OK here is correct syntax thanks a lot to #Tiberiu-IonuČ› Stan
$sql = $this->connection->query("SELECT * FROM ".TBL_USERS."
LEFT JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".country = ".TBL_COUNTRIES.".country_id
LEFT JOIN ".TBL_COUNTRIES." AS ".TBL_COUNTRIES."_b ON ".TBL_USERS.".bcountry=".TBL_COUNTRIES."_b.country_id
INNER JOIN ".TBL_USER_LEVEL." ON ".TBL_USERS.".userlevel = ".TBL_USER_LEVEL.".user_level_id
LEFT JOIN ".TBL_STATES." ON ".TBL_USERS.".states = ".TBL_STATES.".states_id
LEFT JOIN ".TBL_STATES." AS ".TBL_STATES."_b ON ".TBL_USERS.".bstates=".TBL_STATES."_b.states_id
WHERE ".TBL_USERS.".username = '$username'");
You can use OR and AND inside join statements.
I do it all the time.
Here's one that works:
LEFT JOIN `currencies` ON (
`currency_foreign`=`invoice_entry_amount_currency`
AND
`currency_reference`='".$strTransformToCurrency."'
AND
`currency_date`=FLOOR(`invoice_paid_timestamp`/86400)*86400
)
Problem is here:
INNER JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".country = ".TBL_COUNTRIES.".country_id
INNER JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".bcountry = ".TBL_COUNTRIES.".country_id
MySQL can't decide on which relation to join the countries table (because MySQL thinks .country and .countryb can be different - so after join it doesn't know which columns will be returned or used for conditions and ordering).
Try it like this:
INNER JOIN ".TBL_COUNTRIES." ON ".TBL_USERS.".country = ".TBL_COUNTRIES.".country_id
INNER JOIN ".TBL_COUNTRIES." AS ".TBL_COUNTRIES."_b ON ".TBL_USERS.".bcountry=".TBL_COUNTRIES."_b.country_id
In case you have really big tables, do an EXPLAIN with the full final query including conditions and ordering to find out if MySQL is doing a table copy because of "TBL_USERS AS TBL_USERS_B".
You cannot use constructs like JOIN table ON field OR another_field = expression, unless referenced field is of boolean type.
You should use constructs that will return boolean result, in your case something like this:
$sql = $this->connection->query("SELECT * FROM $TBL_USERS
JOIN $TBL_USER_LEVEL ON $TBL_USERS.userlevel = $TBL_USER_LEVEL.user_level_id
JOIN $TBL_COUNTRIES ON $TBL_USERS.country = $TBL_COUNTRIES.country_id
OR $TBL_USERS.bcountry = $TBL_COUNTRIES.country_id
JOIN $TBL_STATES ON $TBL_USERS.states = $TBL_STATES.states_id
OR $TBL_USERS.bstates = $TBL_STATES.states_id
WHERE $TBL_USERS.username = '$username'");
I have also used the variable directly, otherwise lots of string concatenations looks messy.
And your query as is now is open for SQLi.

Doctrine 2 QueryBuilder vs Handcoded DQL - different results

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

Categories