I switched the code to Andrews solution:
SELECT s1.biz_name, s1.biz_info, s1.e_address, s1.e_city, s1.e_state,
s1.e_postal, s1.e_zip_full, s1.loc_LAT_centroid, s1.loc_LONG_centroid,
s1.biz_phone, s1.biz_phone_ext, s1.biz_fax, s1.biz_email, s1.web_url,
s2.upc as upc2, s2.retailprice as retailprice2, s2.dollar_sales as
dollar_sales2, s2.dollar_sales_ly as dollar_sales_ly2, s2.todaydate as
todaydate2, s2.datetimesql as datetimesql2, s2.shelfposition as
shelfposition2, s2.reg_sale as reg_sale2, s2.representative as
representative2, s2.notes as notes2, s3.upc as upc3, s3.retailprice as
retailprice3, s3.dollar_sales as dollar_sales3, s3.dollar_sales_ly as
dollar_sales_ly3, s3.todaydate as todaydate3, s3.datetimesql as
datetimesql3, s3.shelfposition as shelfposition3, s3.reg_sale as reg_sale3,
s3.representative as representative3, s3.notes as notes3, s4.upc as upc4,
s4.retailprice as retailprice4, s4.dollar_sales as dollar_sales4,
s4.dollar_sales_ly as dollar_sales_ly4, s4.todaydate as todaydate4,
s4.datetimesql as datetimesql4, s4.shelfposition as shelfposition4,
s4.reg_sale as reg_sale4, s4.representative as representative4, s4.notes as
notes4, s5.upc as upc5, s5.retailprice as retailprice5, s5.dollar_sales as
dollar_sales5, s5.dollar_sales_ly as dollar_sales_ly5, s5.todaydate as
todaydate5, s5.datetimesql as datetimesql5, s5.shelfposition as
shelfposition5, s5.reg_sale as reg_sale5, s5.representative as
representative5, s5.notes as notes5
FROM allStores AS s1
LEFT OUTER JOIN storeCheckRecords AS s2
ON s1.e_address = s2.e_address AND s2.upc = '650637119004'
LEFT OUTER JOIN storeCheckRecords AS s3
ON s1.e_address = s3.e_address AND s3.upc = '650637119011'
LEFT OUTER JOIN storeCheckRecords AS s4
ON s1.e_address = s4.e_address AND s4.upc = '650637374007'
LEFT OUTER JOIN storeCheckRecords AS s5
ON s1.e_address = s5.e_address AND s5.upc = '650637374014'
WHERE s2.e_address IS NOT NULL
OR s3.e_address IS NOT NULL
OR s4.e_address IS NOT NULL
OR s5.e_address IS NOT NULL
Here is the new error: Invalid query: Too many tables; MySQL can only use 61 tables in a join
Any other ideas? Thanks for the help.
Could be related to
MySQL bug #41156, List of derived tables acts like a chain of mutually-nested subqueries.
The bug log indicates it was verified against MySQL 5.0.72, 5.1.30, and 6.0.7.
Fixed in MySQL 5.1.37, MySQL 5.4.2 (which became 5.5.something), and NDB 7.1.0.
Regarding your redesigned query in the question above:
Pivot queries can be tricky. You can use the method suggested by Andrew in his answer. If you search for many UPC values, you need to write application code to build the SQL query, appending as many JOIN clauses as the number of UPC values you're searching for.
MySQL does have a limit on the number of joins that can be done in a single query, but the example you should doesn't reach the limit. That is, the query you show does work.
I assume that you're showing an example query searching for four UPC codes, whereas your app may construct the query dynamically for a greater number of UPC codes, and that may be more than 61 sometimes.
It looks like the goal of your query is to return stores that has at least one of the listed UPC codes. You can do that more simply in the following query:
SELECT DISTINCT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
ON s.e_address = cr.e_address
AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
You can use this method in other ways, for example to find stores that have all four of the UPC's:
SELECT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
ON s.e_address = cr.e_address
AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
GROUP BY s.e_address
HAVING COUNT(DISTINCT upc) = 4;
Or to find stores that some but not all four of the UPC's:
SELECT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
ON s.e_address = cr.e_address
AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
GROUP BY s.e_address
HAVING COUNT(DISTINCT upc) < 4;
Or to find stores that lack all four of the UPC's:
SELECT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
ON s.e_address = cr.e_address
AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
WHERE cr.e_address IS NULL;
You still have to write some code to build this query, but it's a bit easier to do, and it doesn't exceed any limits on the number of joins or subqueries you can run.
This should give you the same results without using subqueries:
SELECT s1.biz_name,
...
s2.upc AS upc2,
...
s3.upc AS upc3,
...
s4.upc AS upc4,
...
s5.upc AS upc5,
...
FROM allStores AS s1
LEFT OUTER JOIN storeCheckRecords AS s2 ON s1.e_address = s2.e_address
LEFT OUTER JOIN storeCheckRecords AS s3 ON s1.e_address = s3.e_address
LEFT OUTER JOIN storeCheckRecords AS s4 ON s1.e_address = s4.e_address
LEFT OUTER JOIN storeCheckRecords AS s5 ON s1.e_address = s5.e_address
WHERE (s2.e_address IS NOT NULL
OR s3.e_address IS NOT NULL
OR s4.e_address IS NOT NULL
OR s5.e_address IS NOT NULL)
AND (s2.upc = '650637119004' OR s2.upc IS NULL)
AND (s3.upc = '650637119011' OR s3.upc IS NULL)
AND (s4.upc = '650637374007' OR s4.upc IS NULL)
AND (s5.upc = '650637374014' OR s5.upc IS NULL)
I would simplify to just get all the elements first with a simple WHERE IN clause... You appear to be doing a pivot table to show T1 to T2 to T3 to T4 to T5. If you get all the data in individual rows, then you can have STATIC columns across the top showing the details per row under each other.
SELECT
t1.brand,
t1.biz_name,
t1.biz_info,
t1.e_address,
t1.e_city,
t1.e_state,
t1.e_postal,
t1.e_zip_full,
t1.loc_LAT_centroid,
t1.loc_LONG_centroid,
t1.biz_phone,
t1.biz_phone_ext,
t1.biz_fax,
t1.biz_email,
t1.web_url,
t1.upc,
t1.retailprice,
t1.dollar_sales,
t1.dollar_sales_ly,
t1.todaydate,
t1.datetimesql,
t1.shelfposition,
t1.reg_sale,
t1.representative,
t1.notes
FROM
storeCheckRecords as t1
WHERE
t1.upc IN ( '650637119004', '650637119011', '650637374007', '650637374014')
such as..
Brand Bus Addr UPC Retail$ Sales Notes
xyz Bus Name UPC ... etc... Cur Yr
Bus Info Shelf Info Last Yr
Address, (Cit/State/Zip)
Lat / Long
Phone / Fax
Email / Web
----
Next Entry
Does it really matter that exact same address is the same as opposed to who carries an item? What if one entry is "123 Main St", another is "123B Main St", and "123 Main St - Suite B", you wouldn't find a match.
Additionally, you mention some having up to 75 UPC codes... Put those in a separate table and use that as the first table joined to the "StoreCheckRecords" and get them all... instead of manually keying all the columns suffixed from 2 to 75... or however many on the next run being only 17, and yet another 4... I think you may be too fixed into what you are trying to get out from the data.
You could even GROUP by the common "e_address" you originally wanted the matches based on and provide that group as a break between sections reported to the user.
Related
Overview
I have two tables as can be seen below:
user_planes
----------------------------------
|id |user_id|plane_id|fuel|status|
----------------------------------
| 2 1 1 1 Ready |
----------------------------------
shop_planes
------------------------
|id |name|fuel_capacity|
------------------------
| 1 bob 3 |
------------------------
Foreign Key Primary Key
user_planes.plane_id <-> shop_planes.id
I want to be able to get every field (SELECT *) in user_planes and name and fuel_capacity based on the following criteria:
WHERE user_planes.user_id = ? - Parameter which will be added to the query through PHP.
WHERE user_planes.status = 'Ready'
WHERE user_planes.fuel < shop_planes.fuel_capacity
The Issue and My Attempts
I've tried JOIN however it retrieves data which doesn't fit that criteria, meaning it gets extra data which is from shop_planes and not user_planes.
SELECT * FROM `user_planes` WHERE fuel IN (SELECT shop_planes.fuel_capacity FROM shop_planes WHERE fuel < shop_planes.fuel_capacity) AND user_planes.user_id = 1 AND status = 'Ready'
and
SELECT * FROM `user_planes` INNER JOIN `shop_planes` ON user_planes.fuel < shop_planes.fuel_capacity AND user_planes.user_id = 1 AND user_planes.status = 'Ready'
I've searched Stackoverflow and looked through many questions but I've not been able to figure it.
I've looked up many tutorials but still can't get the desired result.
The desired result is that the query should use the data stored in user_planes to retrieve data from shop_planes while at the same time not getting any excess data from shop_planes.
Disclaimer
I really struggle using JOIN queries, I could use multiple separate queries however I wish to optimise my queries hence I'm trying to bring it in to one query.
If their isn't clarity in the question, please do say, I'll update it to the best of my ability.
Note - Is there an easy query builder option available either through phpmyadmin or an alternative software?
Thanks in advance.
Your last attempt was not a bad one, the only thing you missed there was the join criteria you described at the beginning of your post. I also moved the other filters to the where clause to better distinguish between join condition and the filters.
SELECT `user_planes`.*
FROM `user_planes`
INNER JOIN `shop_planes` ON user_planes.plane_id = shop_planes.id
WHERE user_planes.fuel < shop_planes.fuel_capacity AND user_planes.user_id = 1 AND user_planes.status = 'Ready'
First you need the base JOIN
SELECT up.* -- only user_plane fields
FROM shop_planes sp -- CREATE alias for table or field
JOIN user_planes up
ON sp.id = up.plane_id
Case 1: apply a filter in where condition with php parameter.
SELECT up.*
FROM shop_planes sp
JOIN user_planes up
ON sp.id = up.plane_id
WHERE up.user_id = ?
Case 2: apply a filter in where condition with string constant
SELECT up.*
FROM shop_planes sp
JOIN user_planes up
ON sp.id = up.plane_id
WHERE user_planes.status = 'Ready'
Case 3: aply filter comparing fields from both tables
SELECT up.*
FROM shop_planes sp
JOIN user_planes up
ON sp.id = up.plane_id
WHERE up.fuel < sp.fuel_capacity
Try something like:
SELECT
up.id AS User_Plane_ID
, up.[user_id]
, up.plane_id
, up.fuel
, up.[status]
, sp.name AS shop_Plane_Name
, sp.fuel_capacity AS shop_Plane_Fuel_Capacity
FROM User_Planes up
INNER JOIN Shop_Planes sp ON up.plane_id = sp.id
AND up.fuel < sp.Fuel_Capacity
WHERE up.[status] = 'Ready'
AND up.[user_id] = ?
Definitely find a tutorial for JOINs, and don't use SELECT *. With SELECT *, you may end up querying much more than you actually need and it can cause problems if the table changes. You'll enjoy your day much more if you explicitly name the columns you want in your query.
I've aliased some of the columns (with AS) since some of those column names may be reserved words. I've also moved the JOIN criteria to include a filter on fuel
I have a rather big SQL statement that I am working with in MS Access 2010. Here it goes:
SELECT
W.ID AS wid,
W.wpt_ty AS ty,
W.wpt_num AS num,
W.wpt_nxt AS nxt,
W.latdeg AS lat,
W.londeg AS lon,
W.alt AS alt,
W.mission_id AS mid,
W.ctg1 AS ctg1,
W.ctg2 AS ctg2,
W.ctg3 AS ctg3,
W.ctg4 AS ctg4,
W.wpt_index AS indx,
W.vel AS vel,
W.tu AS tu,
R.route_num AS rnum,
R.AC_num AS ac,
R.route_type AS rtype,
R.LastUpdatedOn AS d8,
R.LastUpdatedBy AS auth,
R.flight_wpt_count AS wfcount,
M.mission_name AS msnName,
V.Description AS vstatus,
R.disallowed_reason_id AS did,
CW.wpt_num AS c1num,
CR.matching_route_id AS c1mrid,
CW.wpt_index AS c1indx,
CRU.runway_name AS c1rnwy,
CR.route_num AS c1rnum
FROM Validation AS V
(RIGHT JOIN Runways AS CRU
INNER JOIN (Routes CR
INNER JOIN Waypoints CW ON CR.ID = CW.route_id)
ON Runways.ID = Routes.runway_id
INNER JOIN ((Missions as M
INNER JOIN Routes AS R ON M.ID = R.mission_id)
INNER JOIN Waypoints AS W ON (R.ID = W.route_id)
AND (M.ID = W.mission_id)) ON
V.ID = R.validated
WHERE (((R.matching_route_id)=307543) AND ((R.validated) <> 0 ))
AND (((CW.mission_id)=mid) AND ((CW.wpt_num) = (ctg1))))
If you look at the bottom, you can see am I referencing the values ctg1 and mid on a Right Join while the Inner Joins reference other literal values. Eventually I will want to do the same for ctg2, ctg3, and ctg4
Right now I am running these as 2 separate queries but finding it to be way too slow. If I can join combine the queries (sort of like how I am showing here) it could speed things up greatly. But I am at a loss for how to:
Using select values earlier in said query from the Inner/Left join and push them into values needed on the Right join.
I may be using joins incorrectly, but I thought they had to do with combining data from possible the same tables, just on different pivot points.
How to use the MS Access GUI to help write a query like this.
I know this is for MS Access but I am tagging for MySQL just in case there are similar queries there which can be ported to MS Access?
Have you tried using UNION for this?
It would allow you to execute this query (As two queries, which you mentioned as a possibility), and join the results for your output.
Be warned, it will eat up (only show one of) your duplicates in the results set.
I also suggest reading up on the different types of joins for your own benefit, in the following answer:
MYSQL Joins
So, this query is currently used in a webshop to retrieve technical data about articles.
It has served its purpose fine except the amount of products shown have increased lately resulting in unacceptable long loading times for some categories.
For one of the worst pages this (and some other queries) get requested about 80 times.
I only recently learned that MySQL does not optimize sub-queries that don't have a depending parameter to only run once.
So if someone could help me with one of the queries and explain how you can replace the in's and exists's to joins, i will probably be able to change the other ones myself.
select distinct criteria.cri_id, des_texts.tex_text, article_criteria.acr_value, article_criteria.acr_kv_des_id
from article_criteria, designations, des_texts, criteria, articles
where article_criteria.acr_cri_id = criteria.cri_id
and article_criteria.acr_art_id = articles.art_id
and articles.art_deliverystatus = 1
and criteria.cri_des_id = designations.des_id
and designations.des_lng_id = 9
and designations.des_tex_id = des_texts.tex_id
and criteria.cri_id = 328
and article_criteria.acr_art_id IN (Select distinct link_art.la_art_id
from link_art, link_la_typ
where link_art.la_id = link_la_typ.lat_la_id
and link_la_typ.lat_typ_id = 17484
and link_art.la_ga_id IN (Select distinct link_ga_str.lgs_ga_id
from link_ga_str, search_tree
where link_ga_str.lgs_str_id = search_tree.str_id
and search_tree.str_type = 1
and search_tree.str_id = 10132
and EXISTS (Select *
from link_la_typ
where link_la_typ.lat_typ_id = 17484
and link_ga_str.lgs_ga_id = link_la_typ.lat_ga_id)))
order by article_criteria.acr_value
I think this one is the main badguy with sub-sub-sub-queries
I just noticed i can remove the last exist and still get the same results but with no increase in speed, not part of the question though ;) i'll figure out myself whether i still need that part.
Any help or pointers are appreciated, if i left out some useful information tell me as well.
I think this is equivalent:
SELECT DISTINCT c.cri_id, dt.tex_text, ac.acr_value, ac.acr_kv_des_id
FROM article_criteria AS ac
JOIN criteria AS c ON ac.acr_cri_id = c.cri_id
JOIN articles AS a ON ac.acr_art_id = a.art_id
JOIN designations AS d ON c.cri_des_id = d.des_id
JOIN des_texts AS dt ON dt.tex_id = d.des_tex_id
JOIN (SELECT distinct la.la_art_id
FROM link_art AS la
JOIN link_la_typ AS llt ON la.la_id = llt.lat_la_id
JOIN (SELECT DISTINCT lgs.lgs_ga_id
FROM link_ga_str AS lgs
JOIN search_tree AS st ON lgs.lgs_str_id = st.str_id
JOIN link_la_typ AS llt ON lgs.lgs_ga_id = llt.lat_ga_id
WHERE st.str_type = 1
AND st.str_id = 10132
AND llt.lat_typ_id = 17484) AS lgs
ON la.la_ga_id = lgs.lgs_ga_id
WHERE llt.lat_typ_id = 17484) AS la
ON ac.acr_art_id = la.la_art_id
WHERE a.art_deliverystatus = 1
AND d.des_lng_id = 9
AND c.cri_id = 328
ORDER BY ac.acr_value
All the IN <subquery> clauses can be replaced with JOIN <subquery>, where you then JOIN on the column being tested equaling the column returned by the subquery. And the EXISTS test is converted to a join with the table, moving the comparison in the subquery's WHERE clause into the ON clause of the JOIN.
It's probably possible to flatten the whole thing, instead of joining with subqueries. But I suspect performance will be poor, because this won't reduce the temporary tables using DISTINCT. So you'll get combinatorial explosion in the resulting cross product, which will then have to be reduced at the end with the DISTINCT at the top.
I've converted all the implicit joins to ANSI JOIN clauses, to make the structure clearer, and added table aliases to make things more readable.
In general, you can convert a FROM tab1 WHERE ... val IN (SELECT blah) to a join like this.
FROM tab1
JOIN (
SELECT tab1_id
FROM tab2
JOIN tab3 ON whatever = whatever
WHERE whatever
) AS sub1 ON tab1.id = sub1.tab1_id
The JOIN (an inner join) will drop the rows that don't match the ON condition from your query.
If your tab1_id values can come up duplicate from your inner query, use SELECT DISTINCT. But don't use SELECT DISTINCT unless you need to; it is costly to evaluate.
Here is a brief explanation of what I'm trying to accomplish; my query follows below.
There are 4 tables and 1 view which are relevant for this particular query (sorry the names look messy, but they follow a strict convention that would make sense if you saw the full list):
Performances may have many Performers, and those associations are stored in PPerformer. Fans can have favorites, which are stored in Favorite_Performer. The _UpcomingPerformances view contains all the information needed to display a user-friendly list of upcoming performances.
My goal is to select all the data from _UpcomingPerformances, then include one additional column that specifies whether the given Performance has a Performer which the Fan added as their favorite. This involves selecting the list of Performers associated with the Performance, and also the list of Performers who are in Favorite_Performer for that Fan, and intersecting the two arrays to determine if anything is in common.
When I execute the below query, I get the error #1054 - Unknown column 'up.pID' in 'where clause'. I suspect it's somehow related to a misuse of Correlated Subqueries but as far as I can tell what I'm doing should work. It works when I replace up.pID (in the WHERE clause of t2) with a hard-coded number, and yes, pID is an existing column of _UpcomingPerformances.
Thanks for any help you can provide.
SELECT
up.*,
CASE
WHEN EXISTS (
SELECT * FROM (
SELECT RID FROM Favorite_Performer
WHERE FanID = 107
) t1
INNER JOIN
(
SELECT r.ID as RID
FROM PPerformer pr
JOIN Performer r ON r.ID = pr.Performer_ID
WHERE pr.Performance_ID = up.pID
) t2
ON t1.RID = t2.RID
)
THEN "yes"
ELSE "no"
END as pText
FROM
_UpcomingPerformances up
The problem is scope related. The nested Selects make the up table invisible inside the internal select. Try this:
SELECT
up.*,
CASE
WHEN EXISTS (
SELECT *
FROM Favorite_Performer fp
JOIN Performer r ON fp.RID = r.ID
JOIN PPerformer pr ON r.ID = pr.Performer_ID
WHERE fp.FanID = 107
AND pr.Performance_ID = up.pID
)
THEN 'yes'
ELSE 'no'
END as pText
FROM
_UpcomingPerformances up
select
b.entry_id,
b.assign_id,
a.profile_type,
a.profile_id,
a.profile_name,
a.profile_status,
b.entry_type,
b.assign_id,
c.chapter_name,
d.section_name,
h.group_name,
i.programme_name,
k.subjectprogramme_name,
j.masterprogramme_name,
l.developmentprogramme_name
from profile_master a
left join profile_assign b on (a.profile_id = b.profile_id)
left join chapter_master c
on (b.entry_id = c.chapter_id and b.entry_type='chapter')
left join section_master d
on (b.entry_id = d.section_id and b.entry_type='section')
left join group_master h
on (b.entry_id = h.group_id and b.entry_type='Group'
and h.year_id='".$this->year."')
left join programme_master i
on (b.entry_id = i.programme_id and b.entry_type='Programme'
and i.year_id='".$this->year."')
left join subjectprogramme_master k
on (b.entry_id = k.subjectprogramme_id and b.entry_type='subjectProgramme'
and k.year_id='".$this->year."')
left join masterprogramme_master j
on (b.entry_id = j.masterprogramme_id and b.entry_type='masterProgramme'
and j.year_id='".$this->year."')
left join developmentprogramme_master l
on (b.entry_id = l.developmentprogramme_id
and b.entry_type='developmentProgramme')
1) Get rid of where coditions from left join. Use WHERE clause for filtering
2) I guess UNION or 7 queries (by each entity separetely) will be much better in your case
This is a hard question to answer without having direct access to the database, so I'll try a general answer!
Use "explain" on this query to see if MySQL suggests some indexes. No doubt it'll suggest a few, because you're accessing a few columns several times, and oftentimes indexes will improve even the slowest OUTER JOIN
You're using lots of checks against $this->year, so that would suggest some composite indexes where e.g. the programme_id and the year_id are both in the same index
Of course, there are solutions that might depend on how you're using the output, e.g.:
If this query is run frequently enough to be a problem for users waiting for it, but infrequently enough for latency not to be an issue (e.g. it's ok to run it based on last night's data), you could run it overnight and cache the results.
You really only do a join when a condition is passed, I suggest doing subselects like so:
SELECT
b.entry_id,
b.assign_id,
a.profile_type,
a.profile_id,
a.profile_name,
a.profile_status,
b.entry_type,
b.assign_id,
CASE b.entry_type
WHEN 'chapter' THEN SELECT(c.chapter_name FROM c WHERE b.entry_id = c.chapter_id)
WHEN 'section' THEN SELECT(d.section_name FROM d WHERE b.entry_id = d.section_id)
WHEN ....
END as name
from profile_master a
left join profile_assign b on (a.profile_id = b.profile_id)
If you insist on having the output be the same, then you need to wrap this select in a outer select like so:
SELECT
entry_id, assign_id, ......
, CASE entry_type WHEN 'chapter' THEN name ELSE null END as chapter_name
, CASE entry_type WHEN 'section' THEN name ELSE null END as section_name
FROM
(select statement like above) sub