how to fetch multiple data from mysql after joining multiple tables? - php

Please help me out in fetching into rows.
$results = mysqli_query($db, "SELECT base.*, charge.id, cloud.id, mask.id, sense.id.*
FROM base INNER JOIN charge ON base.id = charge.id
INNER JOIN cloud ON base.id = cloud.id
INNER JOIN mask ON base.id = mask.id
INNER JOIN sense ON base.id = sense.id ");
I'm getting an error a {Error: Unknown table 'sense.id'}. Even though there is an error.
How to correct this?

please try sense.* inplace of sense.id.*
"SELECT base.*, charge.id, cloud.id, mask.id, sense.*
FROM base INNER JOIN charge ON base.id = charge.id
INNER JOIN cloud ON base.id = cloud.id
INNER JOIN mask ON base.id = mask.id
INNER JOIN sense ON base.id = sense.id "

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
");

MYSQL Inner Join speed issue

I have been having mega issues with this query.
It's a file that's run by cron every 24 hours and runs conditional checks against all members in the database.
Apparently it crashes the MYSQL server (takes 2 hours to execute?).
Every table has primary keys set on rows foo_id & foo_uid so the joins are indexed under PRIMARY and as such should be good for speed.
please help, this is killing me.
$members = new WA_MySQLi_RS("members", $alpha, 1);
$members->setQuery("SELECT
registration.*,
child_base_survey.*,
child_base_scas.*,
child_base_smqf.*,
parent_base_survey.*,
parent_base_ippa.*,
parent_base_eac.*,
parent_base_scas.*,
parent_base_smqf.*,
parent_base_eval.*,
user_access_level.*,
parent_one_month_survey.*,
parent_one_month_ippa.*,
parent_one_month_eac.*,
parent_one_month_eval.*,
child_three_month_survey.*,
child_three_month_scas.*,
child_three_month_smqf.*,
parent_three_month_survey.*,
parent_three_month_scas.*,
parent_three_month_smqf.*,
parent_three_month_eval.*,
cron.*
FROM registration
INNER JOIN child_base_survey ON registration.rego_parent_uid = child_base_survey.child_base_survey_uid
INNER JOIN child_base_scas ON child_base_survey.child_base_survey_uid = child_base_scas.child_base_scas_uid
INNER JOIN child_base_smqf ON child_base_scas.child_base_scas_uid = child_base_smqf.child_base_smqf_uid
INNER JOIN parent_base_survey ON child_base_smqf.child_base_smqf_uid = parent_base_survey.parent_base_survey_uid
INNER JOIN parent_base_ippa ON parent_base_survey.parent_base_survey_uid = parent_base_ippa.parent_base_ippa_uid
INNER JOIN parent_base_eac ON parent_base_ippa.parent_base_ippa_uid = parent_base_eac.parent_base_eac_uid
INNER JOIN parent_base_scas ON parent_base_eac.parent_base_eac_uid = parent_base_scas.parent_base_scas_uid
INNER JOIN parent_base_smqf ON parent_base_scas.parent_base_scas_uid = parent_base_smqf.parent_base_smqf_uid
INNER JOIN parent_base_eval ON parent_base_smqf.parent_base_smqf_uid = parent_base_eval.parent_base_eval_uid
INNER JOIN user_access_level ON parent_base_eval.parent_base_eval_uid = user_access_level.user_access_level_uid
INNER JOIN parent_one_month_survey ON user_access_level.user_access_level_uid = parent_one_month_survey.parent_one_month_survey_uid
INNER JOIN parent_one_month_ippa ON parent_one_month_survey.parent_one_month_survey_uid = parent_one_month_ippa.parent_one_month_ippa_uid
INNER JOIN parent_one_month_eac ON parent_one_month_ippa.parent_one_month_ippa_uid = parent_one_month_eac.parent_one_month_eac_uid
INNER JOIN parent_one_month_eval ON parent_one_month_eac.parent_one_month_eac_uid = parent_one_month_eval.parent_one_month_eval_uid
INNER JOIN child_three_month_survey ON parent_one_month_eval.parent_one_month_eval_uid = child_three_month_survey.child_three_month_survey_uid
INNER JOIN child_three_month_scas ON child_three_month_survey.child_three_month_survey_uid = child_three_month_scas.child_three_month_scas_uid
INNER JOIN child_three_month_smqf ON child_three_month_scas.child_three_month_scas_uid = child_three_month_smqf.child_three_month_smqf_uid
INNER JOIN parent_three_month_survey ON child_three_month_smqf.child_three_month_smqf_uid = parent_three_month_survey.parent_three_month_survey_uid
INNER JOIN parent_three_month_scas ON parent_three_month_survey.parent_three_month_survey_uid = parent_three_month_scas.parent_three_month_scas_uid
INNER JOIN parent_three_month_smqf ON parent_three_month_scas.parent_three_month_scas_uid = parent_three_month_smqf.parent_three_month_smqf_uid
INNER JOIN parent_three_month_eval ON parent_three_month_smqf.parent_three_month_smqf_uid = parent_three_month_eval.parent_three_month_eval_uid
INNER JOIN cron ON parent_three_month_eval.parent_three_month_eval_uid = cron.cron_uid WHERE registration.rego_parent_uid = ?");
$members->bindParam("s", "" . ((isset($_SESSION["rego_parent_uid"])) ? $_SESSION["rego_parent_uid"] : "") . "", "-1"); //WAQB_Param1
$members->execute();
?>
Run them as separate queries, from a cursory glance, it looks like you are just joining every possible table with data associated with a reference to an id value. Your results are going to be the cross product of every matching row in each table. For N tables, each row in table X will be repeated r0*r1*r2*....rX-1*rX+1*...rN. With the 23 tables there, if each only had 2 rows, you'd have nearly 8.4 million rows in your results.

JOIN multiple tables in single query using LEFT OUTER JOIN

I'm having difficulties using the LEFT OUTER JOIN in multiple tables.
My tables are: countries, customer_info, package_types, service_types and shipping_info
This is my code so far:
$sql = "SELECT shipping_info.shipping_id, shipping_info.weight, shipping_info.width, shipping_info.height, shipping_info.length, shipping_info.cost, shipping_info.status,
service_types.service, package_types.package_type, countries1.country AS fromCountry, countries2.country AS toCountry, countries3.country AS resiCountry, customer_info.name, customer_info.address
, customer_info.city, customer_info.postcode, customer_info.zipcode, customer_info.phone, customer_info.email, customer_info.country
FROM shipping_info
LEFT OUTER JOIN service_types ON shipping_info.service_type = service_types.serviceType_id
LEFT OUTER JOIN package_types ON shipping_info.package_type = package_types.packageType_id
LEFT OUTER JOIN customer_info ON shipping_info.customer_id = customer_info.customer_id
LEFT OUTER JOIN countries AS countries1 ON shipping_info.from_loc = countries1.country_id
LEFT OUTER JOIN countries AS countries2 ON shipping_info.to_loc= countries2.country_id
LEFT OUTER JOIN countries AS countries3 ON shipping_info.to_id = countries3.country_id";
$statement = $con_db->query($sql);
$result = $statement->fetchAll();
I'm getting a fatal error in my final line and I believe that's because $result is null. But I'm unable to figure out the error.
Appreciate any help.
It may be your query has following manual error:
tablename or fieldname mismatch

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