SUBQUERY and INNER JOIN - differences between MySQL and SQL - mysql

This is a simple query I have used its like (using LIMIT 1 rather that TOP 1) various times in my own MySQL Database:
SELECT
j1.status AS "Status",
j1.number AS "Number",
(
SELECT TOP 1
i2.invoicedDate
FROM invoices AS i2
INNER JOIN jobs AS j2 ON i2.jobKey = j2.id
WHERE
j1.id=j2.id
AND
j2.status = 'INVOICED'
) AS "Invoiced Date"
FROM jobs AS j1
Lets say there are 183,000 rows in the om.jobGroup table, in MySQL the result would return all 183,000 results - and if nothing matched within the subquery it would return NULL
When I run the same query in a Microsoft SQL server; it not only takes significantly longer but only returns like 1700 records?
What is the best way to approach this from an SQL perspective - am I barking up the wrong tree?
Removing the sub query and doing a join on the main query still results in a missmatch of records.

Assuming the ids are unique, I think the query you want in either database is like this:
SELECT j1.status AS "Status", j1.number AS "Number",
(SELECT TOP 1 i2.invoicedDate
FROM invoices i2
WHERE i2.jobKey = j1.id AND j1.status = 'INVOICED'
) Invoiced_Date
FROM jobs j1;
Normally, TOP/LIMIT would be used with an ORDER BY.
Nothing in a subquery in the SELECT is going to change the number of rows.

Related

MySQL queries stuck in "sending data" for 30 seconds after migrating to RDS

This query (along with a few others I think have a related issue) did not take 30 seconds when MySQL was local on the same EC2 instance as the rest of the website. More like milliseconds.
Does anything look off?
SELECT *, chv_images.image_id FROM chv_images
LEFT JOIN chv_storages ON chv_images.image_storage_id =
chv_storages.storage_id
LEFT JOIN chv_users ON chv_images.image_user_id = chv_users.user_id
LEFT JOIN chv_albums ON chv_images.image_album_id = chv_albums.album_id
LEFT JOIN chv_categories ON chv_images.image_category_id =
chv_categories.category_id
LEFT JOIN chv_meta ON chv_images.image_id = chv_meta.image_id
LEFT JOIN chv_likes ON chv_likes.like_content_type = "image" AND
chv_likes.like_content_id = chv_images.image_id AND chv_likes.like_user_id = 1
LEFT JOIN chv_follows ON chv_follows.follow_followed_user_id =
chv_images.image_user_id
LEFT JOIN chv_follows_projects ON
chv_follows_projects.follows_project_project_id =
chv_images.image_project_id LEFT JOIN chv_projects ON
chv_projects.project_id = follows_project_project_id WHERE
chv_follows.follow_user_id='1' OR (follows_project_user_id = 1 AND
chv_projects.project_privacy = "public" AND
chv_projects.project_is_public_upload = 1) GROUP BY chv_images.image_id
ORDER BY chv_images.image_id DESC
LIMIT 0,15
And this is what EXPLAIN shows:
Thank you
Update: This query has the same issue. It does not have a GROUP BY.
SELECT *, chv_images.image_id FROM chv_images
LEFT JOIN chv_storages ON chv_images.image_storage_id =
chv_storages.storage_id
LEFT JOIN chv_users ON chv_images.image_user_id = chv_users.user_id
LEFT JOIN chv_albums ON chv_images.image_album_id = chv_albums.album_id
LEFT JOIN chv_categories ON chv_images.image_category_id =
chv_categories.category_id
LEFT JOIN chv_meta ON chv_images.image_id = chv_meta.image_id
LEFT JOIN chv_likes ON chv_likes.like_content_type = "image" AND
chv_likes.like_content_id = chv_images.image_id AND chv_likes.like_user_id = 1
ORDER BY chv_images.image_id DESC
LIMIT 0,15
That EXPLAIN shows several table-scans (type: ALL), so it's not surprising that it takes over 30 seconds.
Here's your EXPLAIN:
Notice the column rows shows an estimated 14420 rows read from the first table chv_images. It's doing a table-scan of all the rows.
In general, when you do a series of JOINs, you can multiple together all the values in the rows column of the EXPLAIN, and the final result is how many row-reads MySQL has to do. In this case it's 14420 * 2 * 1 * 1 * 2 * 1 * 916, or 52,834,880 row-reads. That should put into perspective the high cost of doing several table-scans in the same query.
You might help avoid those table-scans by creating some indexes on these tables:
ALTER TABLE chv_storages
ADD INDEX (storage_id);
ALTER TABLE chv_categories
ADD INDEX (category_id);
ALTER TABLE chv_likes
ADD INDEX (like_content_id, like_content_type, like_user_id);
Try creating those indexes and then run the EXPLAIN again.
The other tables are already doing lookups by primary key (type: eq_ref) or by secondary key (type: ref) so those are already optimized.
Your EXPLAIN shows your query uses a temporary table and filesort. You should reconsider whether you need the GROUP BY, because that's probably causing the extra work.
Another tip is to avoid using SELECT * because it might be forcing the query to read many extra columns that you don't need. Instead, explicitly name only the columns you need.
Is there any indexes in chv_images?
I propose:
CREATE INDEX idx_image_id ON chv_images (image_id);
(Bill's ideas are good. I'll take the discussion a different way...)
Explode-Implode -- If the LEFT JOINs match no more than 1 row, change, for example,
SELECT
...
LEFT JOIN chv_meta ON chv_images.image_id = chv_meta.image_id
into
SELECT ...,
( SELECT foo FROM chv_meta WHERE image_id = chv_images.image_id ) AS foo, ...
If that can be done for all the JOINs, you can get rid of GROUP BY. This will avoid the costly "explode-implode" where JOINs lead to more rows, then GROUP BY gets rid of the dups. (I suspect you can't move all the joins in.)
OR -> UNION -- OR is hard to optimize. Your query looks like a good candidate for turning into UNION, then making more indexes that will become useful.
WHERE chv_follows.follow_user_id='1'
OR (follows_project_user_id = 1
AND chv_projects.project_privacy = "public"
AND chv_projects.project_is_public_upload = 1
)
Assuming that follows_project_user_id is in `chv_images,
( SELECT ...
WHERE chv_follows.follow_user_id='1' )
UNION DISTINCT -- or ALL, if you are sure there won't be dups
( SELECT ...
WHERE follows_project_user_id = 1
AND chv_projects.project_privacy = "public"
AND chv_projects.project_is_public_upload = 1 )
Indexes needed:
chv_follows: (follow_user_id)
chv_projects: (project_privacy, project_is_public_upload) -- either order
But this has not yet handled the ORDER BY and LIMIT. The general pattern for such:
( SELECT ... ORDER BY ... LIMIT 15 )
UNION
( SELECT ... ORDER BY ... LIMIT 15 )
ORDER BY ... LIMIT 15
Yes, the ORDER BY and LIMIT are repeated.
That works for page 1. If you want the next 15 rows, see http://mysql.rjweb.org/doc.php/pagination#pagination_and_union
After building those two sub-selects, look at them; I think you will be able to optimize each one, and may need new indexes because the Optimizer will start with a different 'first' table.

Use subquery in mysql

The query below gives me 2 out of the 3 answers I'm looking for. On the sub-query select I get null instead of no
the 3 possible values for column name isCyl could be blank, yes, no
I'm not sure if the sub-query is the best way to go about it, but I don't know how else to re-state the query.
The schedule table has a series of columns to show what tasks must be completed on an assignment. Related tables store the results of the tasks if they were assigned to be completed. So I need to test if a specific task was scheduled. If so, then I need to see if the results of the task have been recorded in the related table. For brevity I am only showing one of the columns here.
SELECT s.`reckey`,
if(s.cylinders="T",
(select
if(c.areckey is not null,
"yes",
"no"
)
from cylinders c where c.areckey = s.reckey limit 1
)
,""
) as isCyl
from schedule s
where s.assignmentDate between 20161015 and 20161016
order by s.reckey
Use a LEFT JOIN, which returns NULL for columns in the child table when there's no match.
SELECT s.reckey, IF(s.cylinders = "T",
IF(c.areckey IS NOT NULL, 'yes', 'no'),
"") AS isCyl
FROM schedule AS s
LEFT JOIN cylinders AS c ON c.areckey = s.reckey
WHERE s.assignmentDate between 20161015 and 20161016
ORDER BY s.reckey
If there can be multiple rows in cylinders with the same areckey, change it to:
LEFT JOIN (select distinct areckey FROM cylinders) AS c on c.areckey = s.reckey
or use SELECT DISTINCT in the main query.

MYSQL - "#1111 - Invalid use of group function" error

I'm programming a weather station for a school project.
In mysql I have 1 table with my readings and another one with calculated values. I wrote a mysql query to update the second table with the calculated values. When I run this query, I receive this error
1111 - Invalid use of group function
I don't know what I'm doing wrong.
My query:
UPDATE Waarnemingen2 As t1
INNER JOIN (SELECT `Datum_Tijd`,`Temperatuur`,`Luchtvochtigheid`,`Luchtdruk`,`Regenhoeveelheid` FROM Waarnemingen GROUP BY day(`Datum_Tijd`) + hour(`Datum_Tijd`)) as t2
SET t1.`Min. temperatuur` = MIN(`Temperatuur`),
t1.`Gem. temperatuur` = AVG(`Temperatuur`),
t1.`Max. temperatuur` = MAX(`Temperatuur`),
t1.`Min. luchtvochtigheid` = MIN(`Luchtvochtigheid`),
t1.`Gem. luchtvochtigheid` = AVG(`Luchtvochtigheid`),
t1.`Max. luchtvochtigheid` = MAX(`Luchtvochtigheid`),
t1.`Min. luchtdruk` = MIN(`Luchtdruk`),
t1.`Gem. luchtdruk` = AVG(`Luchtdruk`),
t1.`Max. luchtdruk` = MAX(`Luchtdruk`),
t1.`Regen` = SUM(`Regenhoeveelheid`)
The query should take the minimum, maximum and average from the columns "Temperatuur", "Luchtvochtigheid" and "Luchtdruk" from each hour.
Is there someone who can help me?
Your error is because you have unaggregated columns in a SELECT statement that are not in your GROUP by clause.
In a SELECT with a GROUP BY, the only columns you can select are:
aggregations (MAX(Temperatur), MIN(Luchtdruk), etc.)
grouped columns
Otherwise you get this error.
Change your subquery to perform your aggregations and then just assign those aggregated values to your joined table.
(I don't know MySQL syntax, this code won't work in SQL Server because it does UPDATE JOINs a little differently, but the important concept is the aggregation.)
i.e.
UPDATE Waarnemingen2 As t1
INNER JOIN (
SELECT day(`Datum_Tijd`) as day, hour(`Datum_Tijd`) as hour, MIN(Temperatur) as Min_Temperature FROM Waarnemingen GROUP BY day(`Datum_Tijd`) , hour(`Datum_Tijd`)
) t2 on t1.day = t2.day and t1.hour = t2.hour
set
t1.MinTemp = t2.Mintemp

Apply where condition when I have multiple rows for each id (Single table or Join multiple ones)

I have below requirement in mysql/SQL Server
Table Name: basic(pid int,av int,sid int,st int,wid int,wt int)
For each pid, there would be 10 rows (containing sid,st values and wid,wt values for each pid). These sets could be from 1 to 10.
So, for a pid value (example: 3213 and 3214), there will be 10 rows like below
Like the above, there could be millions of records
What am trying to achieve is, I want to get the pid's whose (sid=2 and respective st>=7) and also whose (wid=9 and respective wt>=6)
If I apply this condition, I should get list of pid's which must have two pid's 3213 and 3214.
How can I achieve this using simple sql query or i can divide the table into three like basic1(pid,av), basic_sk(pid,sid,st) and basic_wc(pid,wid,wt)
since I can use pid as reference, I can join .. even I tried using joins, and couldn't achieve the required result.
I used below join -
select t1.pid from basic1 t2
inner join basic_sk t2 on t1.pid=t2.pid
inner join basic_wc t3 on t3.pid=t2.pid
where (((t2.sid=2) and (t2.st>=7)) and ((t3.wid=9) and (t3.wt>=6)))
but no luck.
How about if I have multiple sid and st values in where condition and wid and wt values..
like in sets {sid=2,st>=7} and {sid=4,st>=9}
and {wid=9,wt>=6} and {wid=5,wt>=5}
How can I achieve my requirement using simple sql query ?
Any possibility is fine for me, with one table or multiple tables (using join)
One method is to use aggregation and a having clause;
select b.pid
from basic b
group by p.pid
having sum(case when (b.sid = 2) and (b.st >= 7) then 1 else 0 end) > 0 and
sum(case when (b.wid = 9) and (b.wt >= 6) then 1 else 0 end) > 0;
Each condition in the having clause counts the rows that match each condition. The > 0 ensure that there is at least one row for each.

Mysql, combining and querying results with sub queries or temp tables

I am running into some trouble with the following circumstances:
I have a query that creates two temp tables, and the following select to join them together--
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId
I am then trying to create another column from concatenating a few of the resulting fields and then use that to reference/query against another table. Is there a way to accomplish this in one query? Should I get away from the temp tables?
Thank you again in advance.
update: If I try to alias the combination of the two temp tables I get an error message stating [Err] 1060 - Duplicate column name 'packetDetailsId'
select * from (
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId) as myalias
Another Update: I almost have it working as one query but I get the result "(BLOB)" in the column I concoctenated:
select packet_details.packetDetailsId,products.productId,Credit,AccountNum,OrderStat, CONCAT(products.productId,Credit,'_',OrderStat) as consol from (
select packetDetailsId, GROUP_CONCAT(Credit) AS Credit, GROUP_CONCAT(AccountNum) AS AccountNum, GROUP_CONCAT(OrderStat) AS OrderStat FROM
( SELECT pd_extrafields.packetDetailsId,
CASE WHEN pd_extrafields.ex_title LIKE ('%Credit%')
THEN pd_extrafields.ex_value ELSE NULL END as Credit,
CASE WHEN pd_extrafields.ex_title LIKE ('%Account%')
THEN pd_extrafields.ex_value ELSE NULL END as AccountNum,
CASE WHEN pd_extrafields.ex_title LIKE ('%Existing%')
THEN pd_extrafields.ex_value ELSE NULL END as OrderStat
FROM pd_extrafields )AS TempTab GROUP BY packetDetailsId ) as alias2
INNER JOIN packet_details ON alias2.packetDetailsId = packet_details.packetDetailsId
INNER JOIN sales ON packet_details.packetDetailsId = sales.packetDetailsId
INNER JOIN sold_products ON sales.saleId = sold_products.saleId
INNER JOIN products ON sold_products.productId = products.productId
If I understand correctly, you already have the temporary tables created and you need to "concatenate" the results, using from ... inner join ...
The only possible restriction you may have is that you can only reference your temporary tables once in your from clause; besides that, there are no other restrictions (I frequently use temporary tables as intermediate steps in the creation of my final result).
Tips
Let's say your temp tables are temp_result1 and temp_result2. Both tables have a field packedDetailsId, on which the join will be performed. Remember to create the appropriate indexes on each table; at the very least you need to index packedDetailsId on both tables:
alter table temp_result1
add index PDI(packedDetailsId);
alter table temp_result2
add index PDI(packedDetailsId);
Now, just execute a query with the desired join and concatenation. If concat returns BLOB, then cast the result as char (of course, I'm assuming you need a text string):
select r1.*, r2.*, cast(concat(r1.field1, ',', r2.field2) as char) as data_concat
from temp_result1 as r1
inner join temp_result2 as r2 on r1.packedDetailsId = r2.packedDetailsId;
I see your problem is that GROUP_CONCAT is returning BLOB values... It's normal (MySQL doesn't know a priori how to return the values, so it returns binary data); just use the cast function.
Hope this helps you
so, if the result2 and result are both temp tables, you will have to include the # if local temp table and ## if global temp table
so your statements should be :
SELECT * FROM #result
INNER JOIN #result2 ON #result2.packetDetailsId = #result.packetDetailsId
My Bad. This is only applicable for MS SQL