Make a COUNT on JOIN tables - mysql

I have a query which I want to COUNT the rows. The problem is when I try to count the exact same query it's printing 9 but when I run the query and print the rows it's only 5 (as it should be)
Here is the query that works and show the 5 rows:
$results_quiz = $pdo->query("SELECT
sr.statistic_ref_id,
sr.quiz_id,
sr.user_id,
sr.total_time,
qm.name AS q_name,
qm.category_id,
qm.subcategory_id,
sc.sub_category_id,
sc.sub_category_name AS quiz_type,
pl.time,
pl.points,
COUNT(qs.correct_count) AS count_correct
FROM pro_quiz_statistic_ref AS sr
JOIN pro_quiz_master qm ON qm.id = sr.quiz_id
JOIN pro_quiz_subcategory sc ON sc.sub_category_id = qm.subcategory_id
JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$get_id'
JOIN pro_quiz_statistic qs ON qs.statistic_ref_id = sr.statistic_ref_id
WHERE
sr.user_id = '$get_id' AND
qs.correct_count = '1'
GROUP BY
sr.statistic_ref_id,
qs.correct_count
ORDER BY qm.name ASC");
The above code works like it should but when I use this:
$count_results_quiz = $pdo->query("SELECT COUNT('
sr.statistic_ref_id,
sr.quiz_id,
sr.user_id,
sr.total_time,
qm.name AS q_name,
qm.category_id,
qm.subcategory_id,
sc.sub_category_id,
sc.sub_category_name AS quiz_type,
pl.time,
pl.points,
COUNT(qs.correct_count) AS count_correct')
FROM pro_quiz_statistic_ref AS sr
JOIN pro_quiz_master qm ON qm.id = sr.quiz_id
JOIN pro_quiz_subcategory sc ON sc.sub_category_id = qm.subcategory_id
JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$get_id'
JOIN pro_quiz_statistic qs ON qs.statistic_ref_id = sr.statistic_ref_id
WHERE
sr.user_id = '$get_id' AND
qs.correct_count = '1'
GROUP BY
sr.statistic_ref_id,
qs.correct_count
ORDER BY qm.name ASC")->fetchColumn();
it's printing "9". Anyone knows why?

I found another solution by splitting the tables into two queries/tables. One of them showing more than 0 (at least 1) correct answer and the other one showing 0 correct answers. This way it also fits better to my needs.

Related

Show rows with SUM of two columns with subqueries and multiple JOINS

Hello I'm about to explode.. I have been trying for hours now getting this to work. I have found different solutions for others and tried to rewrite it so it fits to my script but it just wont work as it should.
With reference from Barmar to this post: Join tables with SUM issue in MYSQL
I have got a little way forward but I can't get it to print out what it should.
I have tried a lot of variant and moved around the JOINS etc. but without any luck. This is the query that gets nearest my wanted result:
SELECT
qs.statistic_ref_id,
qs.question_id,
SUM(qs.correct_count) AS count_correct,
qs2.total_count_2 AS total_count,
sr.user_id,
sr.quiz_id,
qq.title AS ny_titel,
qq.category_id,
SUBSTRING(qq.title,1,4) AS get_type_number,
pl.points
FROM pro_quiz_statistic AS qs
JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
JOIN pro_quiz_question qq ON qq.id = qs.question_id
JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'
JOIN ( SELECT
qs3.statistic_ref_id,
qs3.question_id,
SUM(qs3.correct_count + qs3.incorrect_count) AS total_count_2,
sr3.user_id,
sr3.quiz_id,
qq3.title AS ny_titel_2,
qq3.category_id,
SUBSTRING(qq3.title,1,4) AS get_type_number_2,
pl3.points
FROM pro_quiz_statistic AS qs3
JOIN pro_quiz_statistic_ref sr3 ON sr3.statistic_ref_id = qs3.statistic_ref_id
JOIN pro_quiz_question qq3 ON qq3.id = qs3.question_id
JOIN user_points_log pl3 ON pl3.quiz_id = sr3.quiz_id AND pl3.user_id = '$user_id'
WHERE sr3.user_id = '$user_id' AND
qq3.category_id = '3'
GROUP BY get_type_number_2 ORDER BY qs3.question_id
) qs2 ON qs2.statistic_ref_id = sr.statistic_ref_id
WHERE sr.user_id = '$user_id' AND
qq.category_id = '3'
GROUP BY get_type_number ORDER BY qs.question_id
You can see the outcome of this result in this picture (to the right):
whats_printing.jpg
The two first tables is pictures of the queries separately and is what it should print out. Just in one query.
I have tried with these two subqueries:
SELECT
qs.statistic_ref_id,
qs.question_id,
qs2.correct_count AS count_correct,
qs3.total_count_2 AS total_count,
sr.user_id,
sr.quiz_id,
qq.category_id,
SUBSTRING(qq.title,1,4) AS get_type_number,
pl.points
FROM pro_quiz_statistic AS qs
JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
JOIN pro_quiz_question qq ON qq.id = qs.question_id
JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'
JOIN (
SELECT
qs2.statistic_ref_id,
qs2.question_id,
SUM(qs2.correct_count) AS count_correct_2,
sr2.user_id,
sr2.quiz_id,
qq2.category_id,
SUBSTRING(qq2.title,1,4) AS get_type_number_2,
pl2.points
FROM pro_quiz_statistic AS qs2
JOIN pro_quiz_statistic_ref sr2 ON sr2.statistic_ref_id = qs2.statistic_ref_id
JOIN pro_quiz_question qq2 ON qq2.id = qs2.question_id
JOIN user_points_log pl2 ON pl2.quiz_id = sr2.quiz_id AND pl2.user_id = '$user_id'
WHERE sr2.user_id = '$user_id' AND
qq2.category_id = '3'
GROUP BY get_type_number_2
ORDER BY qs2.question_id
) qs2 ON qs2.statistic_ref_id = qs.statistic_ref_id
JOIN ( SELECT
qs3.statistic_ref_id,
qs3.question_id,
SUM(qs3.correct_count + qs3.incorrect_count) AS total_count_2,
sr3.user_id,
sr3.quiz_id,
qq3.category_id,
SUBSTRING(qq3.title,1,4) AS get_type_number_3,
pl3.points
FROM pro_quiz_statistic AS qs3
JOIN pro_quiz_statistic_ref sr3 ON sr3.statistic_ref_id = qs3.statistic_ref_id
JOIN pro_quiz_question qq3 ON qq3.id = qs3.question_id
JOIN user_points_log pl3 ON pl3.quiz_id = sr3.quiz_id AND pl3.user_id = '$user_id'
WHERE sr3.user_id = '$user_id' AND
qq3.category_id = '3'
GROUP BY get_type_number_3
ORDER BY qs3.question_id
) qs3 ON qs3.statistic_ref_id = qs.statistic_ref_id
WHERE sr.user_id = '$user_id' AND
qq.category_id = '3'
GROUP BY get_type_number
ORDER BY qs.question_id
but then it's not printing anything. This works:
SELECT
qs.statistic_ref_id,
qs.question_id,
SUM(qs.correct_count) AS count_correct,
sr.user_id,
sr.quiz_id,
qq.title AS ny_titel,
qq.category_id,
SUBSTRING(qq.title,1,4) AS get_type_number,
pl.points
FROM pro_quiz_statistic AS qs
JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
JOIN pro_quiz_question qq ON qq.id = qs.question_id
JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'
WHERE sr.user_id = '$user_id' AND
qq.category_id = '3'
GROUP BY get_type_number ORDER BY qs.question_id
And also this (if I run them separately):
SELECT
qs.statistic_ref_id,
qs.question_id,
SUM(qs.correct_count + qs.incorrect_count) AS count_correct,
sr.user_id,
sr.quiz_id,
qq.title AS ny_titel,
qq.category_id,
SUBSTRING(qq.title,1,4) AS get_type_number,
pl.points
FROM pro_quiz_statistic AS qs
JOIN pro_quiz_statistic_ref sr ON sr.statistic_ref_id = qs.statistic_ref_id
JOIN pro_quiz_question qq ON qq.id = qs.question_id
JOIN user_points_log pl ON pl.quiz_id = sr.quiz_id AND pl.user_id = '$user_id'
WHERE sr.user_id = '$user_id' AND
qq.category_id = '3'
GROUP BY get_type_number ORDER BY qs.question_id
But how can I combine those two to one query?

Mysql Fetching rows base on date

Here's the code:
SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
ip.dblItemPAmount, MAX(ip.dtmItemPasOf) AS EXR
FROM TBLREQUESTDETAILS RD,tblitem I,tblvendor v,tblitemunit iu,tblitemprice ip`
WHERE RD.strReqDItemCode = I.strItemCode
AND RD.strReqDItemUnitCode = iu.strItemUnitCode
AND RD.strReqDVendCode = v.strVendCode
AND i.strItemCode = ip.strItemPItemCode
and RD.strReqDReqHCode = 'RQST121'
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName, ip.dblItemPAmount
ORDER BY EXR desc ;
AND Here's The result:
What should I do If I want to fetch the current price for each itemname,vendorname and itemunit?? I want to fetch only those rows who's price is the Latest... Help me please those with boxes are the rows that i want to fetch
You can use where in the grouped value (and use explicict join notatio)
SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
ip.dblItemPAmount, ip.dtmItemPasOf AS EXR
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode
WHERE RD.strReqDReqHCode = 'RQST121'
and ( RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName, ip.dtmItemPasOf)
in ( SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
MAX(ip.dtmItemPasOf)
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode
WHERE RD.strReqDReqHCode = 'RQST121'
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName
)
ORDER BY EXR desc ;
(and use explicict join notation .. i think is more readable)

NEGATION INNER JOIN with WHERE

Why the sql code below ignores
WHERE r.tt_schedules_id = '105'
in
SELECT DISTINCT r.load_date, u.url
FROM tt_results r
INNER JOIN url_lp u
ON u.lp_id <> r.lp_id
INNER JOIN tt_schedules s
ON s.tt_schedules_id = r.tt_schedules_id
WHERE r.tt_schedules_id = '105'
AND (DATE(NOW()) - DATE(r.load_date) <= 7)
?
Basically, it returns everything with any
tt_schedules_id
There are 5 tables:
url_lp, tt_results (table having foreign keys in url_lp, tt_schedules and
tt_tags), tt_schedules, tt_tags(not used) and tt_schedules_url_lp_hub
If you draw the scheme on the paper you should be clear.
I fixed it:
SELECT u.url FROM url_lp u
INNER JOIN tt_schedules_url_lp_hub hub
ON hub.lp_id = u.lp_id
INNER JOIN tt_schedules s
ON hub.tt_schedules_id = s.tt_schedules_id
WHERE s.tt_schedules_id = '105'
AND u.lp_id NOT IN
(SELECT r.lp_id FROM tt_results r WHERE r.tt_schedules_id = '105')
Cheers.

Speeding up MySQL Query inc subquery-join?

I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema

MYSQL LEFT JOIN optimization with CASE

I spent some time trying to get working this SELECT with CASE but I failed... (thank to that I'm using COLASCE() now)
How could I optimize this SELECT by using CASE/IF sentences? Is this a fast way to query from different tables selected by a field?
SELECT a.folderid, a.foldername, a.contenttype, COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor, f.descriptor) as descriptor
FROM t_folders a
LEFT JOIN t_files b
ON a.contenttype = 'file' AND a.contentid = b.fileid
LEFT JOIN t_links c
ON a.contenttype = 'link' AND a.contentid = c.linkid
LEFT JOIN t_extfiles d
ON a.contenttype = 'extfile' AND a.contentid = d.extfileid
LEFT JOIN t_videos e
ON a.contenttype = 'video' AND a.contentid = e.videoid
LEFT JOIN t_exams f
ON a.contenttype = 'exam' AND a.contentid = f.examid
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC
Using case statement will not make the query faster in your case, but since you asked for it, below is how it would look like.
SELECT a.folderid, a.foldername, a.contenttype,
(CASE a.contenttype
WHEN 'file' THEN b.descriptor
WHEN 'link' THEN c.descriptor
WHEN 'extfile' THEN d.descriptor
WHEN 'video' THEN e.descriptor
ELSE f.descriptor
END CASE) AS descriptor
FROM t_folders a
LEFT JOIN t_files b ON a.contenttype = 'file' AND a.contentid = b.fileid
LEFT JOIN t_links c ON a.contenttype = 'link' AND a.contentid = c.linkid
LEFT JOIN t_extfiles d ON a.contenttype = 'extfile' AND a.contentid = d.extfileid
LEFT JOIN t_videos e ON a.contenttype = 'video' AND a.contentid = e.videoid
LEFT JOIN t_exams f ON a.contenttype = 'exam' AND a.contentid = f.examid
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC
If each of the t_files, t_links, etc tables has the folder_id field, I would also try doing a UNION on these tables and then left join the result with t_folders to get the folderid and foldername.
The joins have to be done this way because the come out of different tables. You cannot use CASE to switch which table a query is coming out of: the statement has to be parsed, including its data sources, before there are values to compare.
More to the point, CASE returns values, whereas table names are.. I don't know the technical term.. structural components of the query. It's sort of the same reason you can't select out of table "Cool_Stuff" with:
select * from "Cool_" + "Stuff"
Hope this answers your question!
You may do the following
select master.* , COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor, f.descriptor) as descriptor
from
( SELECT a.folderid, a.foldername, a.contenttype
FROM t_folders a
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC ) master
LEFT JOIN t_files b
ON master.contenttype = 'file' AND master.contentid = b.fileid
LEFT JOIN t_links c
ON master.contenttype = 'link' AND master.contentid = c.linkid
LEFT JOIN t_extfiles d
ON master.contenttype = 'extfile' AND master.contentid = d.extfileid
LEFT JOIN t_videos e
ON master.contenttype = 'video' AND master.contentid = e.videoid
LEFT JOIN t_exams f
ON master.contenttype = 'exam' AND master.contentid = f.examid
While minimizing the result on table a, the joins operation can be optmizsed