Having issues with this Mysql query - php

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

Related

Why Huge Join Fails After Specific Table?

I have a huge select query where i have to join more than 85 tables. I keep getting an error when running the query, if I re-run the query when shrinking the overall statement it runs fine.
See a portion of the join below, it does that all the way to table 85:
select $imploded_tables from $apps a
left join $mobile_c0 $m_c0 on $apps.id = $m_c0.id
left join $mobile_c1 $m_c1 on $m_c0.id = $m_c1.id
left join $mobile_c2 $m_c2 on $m_c1.id = $m_c2.id
left join $mobile_c3 $m_c3 on $m_c2.id = $m_c3.id
left join $mobile_c4 $m_c4 on $m_c3.id = $m_c4.id
left join $mobile_c5 $m_c5 on $m_c4.id = $m_c5.id
left join $mobile_c6 $m_c6 on $m_c5.id = $m_c6.id
left join $mobile_c7 $m_c7 on $m_c6.id = $m_c7.id
left join $mobile_c8 $m_c8 on $m_c7.id = $m_c8.id
left join $mobile_c9 $m_c9 on $m_c8.id = $m_c9.id
left join $mobile_c10 $m_c10 on $m_c9.id = $m_c10.id
...
...
Mysql Documentation
The maximum number of tables that can be referenced in a single join is 61.
As per #sf_admin answer the maximum no of join is 61 , so you can do something like below
select imploded_tables from apps a
JOIN
(
SELECT * from mobile_c0
UNION
SELECT * from mobile_c1
....
)tmp
where(tmp.id=a.id)
It might not 100% percent answer your question but this is some work around I did

mysql empty result set when i know data is in my tables

I have 5 tables that i am running a SELECT statement against, when all of the tables have data in them it all works fine but if only 1 table has data in it i get zero results? how can i get the query to work when not all tables are populated with data. proper scratiching my head now, please help!
SELECT admins.username, round1.player, round1.round1_score,
round2.player, round2.round2_score,
round3.player, round3.round3_score,
round4.player, round4.round4_score,
round5.player, round5.round5_score
FROM `admins`, `round1`, `round2`, `round3`, `round4`, `round5`
WHERE admins.username <> 'admin'
AND admins.username = round1.player
AND admins.username = round2.player
AND admins.username = round3.player
AND admins.username = round4.player
AND admins.username = round5.player
GROUP BY admins.username
I'm assuming the admins table is the master table? You need to use outer joins instead:
SELECT a.username, r1.player, r1.round1_score,
r2.player, r2.round2_score, r3.player, r3.round3_score,
r4.player, r4.round4_score, r5.player, r5.round5_score
FROM `admins` a
LEFT JOIN `round1` r1 ON a.username = r1.player
LEFT JOIN `round2` r2 ON a.username = r2.player
LEFT JOIN `round3` r3 ON a.username = r3.player
LEFT JOIN `round4` r4 ON a.username = r4.player
LEFT JOIN `round5` r5 ON a.username = r5.player,
WHERE a.username <> 'admin'
GROUP BY a.username
I'm not sure you really need the group by, in general it's used with aggregation (or sometimes to get distinct records).
The way your query is, is like using inner join so basically if one table has no data you get 0 results. You need to change your query using left join.
try to inner join or cross join tables to get an answer i think it will works
you can read more here or here
you are connecting 5 tables
admins, round1, round2, round3, round4, round5
this is a INNER JOIN and you have only common data
if you want data even your table on right side is empty you can use a LEFT JOIN
SELECT admins.username, round1.player, round1.round1_score,
round2.player, round2.round2_score,
round3.player, round3.round3_score,
round4.player, round4.round4_score,
round5.player, round5.round5_scoreFROM (((((`admins` left join `round1` on admins.username = round1.player)
left join `round2` on admins.username = round2.player)
left join `round3` on admins.username = round3.player)
left join `round4` on admins.username = round4.player)
left join `round5` on admins.username = round5.player)
WHERE admins.username <> 'admin'
GROUP BY admins.username

Left join not working in my prepared statement (mysqli)

I can't get a left join to work in my prepared statement.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id
/*,GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)*/
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
/*
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
*/
WHERE a.ac LIKE ? AND a.flag = ?"
The commented parts are the parts that aren't working.
I have no idea what I'm doing wrong.
EDIT
First of all I forgot that both tables have a column ac, so I've changed the where statement a bit. The left joins are working now, but the part in the select is still not working
So the problem was that I forgot a GROUP BY.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id,
GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
WHERE a.ac LIKE ? AND a.flag = ?
GROUP BY a.auto_id"

Connect multiple tables using LEFT OUTER JOIN

I'm trying to get data from multiple tales using LEFT OUTER JOIN but I'm getting a fatal error.
Table names, field names, db connection are correct.
$sql = "SELECT shipping_info.shipping_id, service1.service, package1.package_type, countries1.country AS fromCountry, countries2.country AS toCountry, countries3.country AS resiCountry, customer1.name,
FROM shipping_info
LEFT OUTER JOIN service_types AS service1 ON shipping_info.service_type = service_types.serviceType_id
LEFT OUTER JOIN package_types AS package1 ON shipping_info.package_type = package_types.packageType_id
LEFT OUTER JOIN customer_info AS customer1 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
ORDER BY shipping_info.order_date DESC";
Fatal error: Call to a member function fetchAll() on a non-object in....
try changing your query to this:
SELECT s1.shipping_id,
s1.service,
p1.package_type,
c1.country fromCountry,
c2.country toCountry,
c3.country resiCountry,
c1.name
FROM shipping_info si
LEFT JOIN service_types s1 ON si.service_type = s1.serviceType_id
LEFT JOIN package_types p1 ON si.package_type = p1.packageType_id
LEFT JOIN customer_info c1 ON si.customer_id = c1.customer_id
LEFT JOIN countries c1 ON si.from_loc = c1.country_id
LEFT JOIN countries c2 ON si.to_loc= c2.country_id
LEFT JOIN countries c3 ON si.to_id = c3.country_id
ORDER BY si.order_date DESC;
you had multiple typos in the query itself with incorrect syntax.. also LEFT OUTER JOIN and LEFT JOIN are exactly the same
Also can you post how you are executing this query? you may have an issue with the actual method for executing it.
I'm not a MySQL expert, but this looks wrong. Consider your first join:
LEFT OUTER JOIN service_types AS service1
ON shipping_info.service_type = service_types.serviceType_id
You give table service_types an alias (a correlation name) of service1, but then don't use it in the ON part of the join. The first thing I would try is either get rid of the correlation name:
LEFT OUTER JOIN service_types
ON shipping_info.service_type = service_types.serviceType_id
...or use it:
LEFT OUTER JOIN service_types AS service1
ON shipping_info.service_type = service1.serviceType_id
Since you're using it in the names of the columns you're actually selecting, I'd go with using it in ON part of the join. Whichever, repeat with package_types and customer_info and then try.
Right off the bat I can see that you have an extra comma just before the FROM clause. This would cause an error and could result in the error you are getting as you would be running fetchAll on a non-object, i.e. the query that wasn't formatted properly.
Your table aliases are pretty long, so you could either shorten them or ditch them altogether and just use the full table name.
SELECT
si.shipping_id,
st.service,
pt.package_type,
c1.country AS fromCountry,
c2.country AS toCountry,
c3.country AS resiCountry,
ci.name
FROM shipping_info si
LEFT JOIN service_types st
ON si.service_type = st.serviceType_id
LEFT JOIN package_types pt
ON si.package_type = pt.packageType_id
LEFT JOIN customer_info ci
ON si.customer_id = ci.customer_id
LEFT JOIN countries c1
ON si.from_loc = c1.country_id
LEFT JOIN countries c2
ON si.to_loc= c2.country_id
LEFT JOIN countries c3
ON si.to_id = c3.country_id
ORDER BY si.order_date DESC;
Also, I've recently moved over to using MySQL Workbench. It's definitely worth checking out. I like it better than PHPMyAdmin. It's a better workflow for me and has cool tools like the reverse engineer tool that will build an ERD for you based on your tables. It's great for testing out your queries before using them in your PHP code.
MySQL Workbench
http://www.mysql.com/products/workbench/

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