MySQL: update table records by sum from another table - mysql

I need to sum all time from "user_answers.time" and update "question.total_time", for all records
note: (user_answers.qid foreign key refer to question.id)
tables structure:
"question" table:
id total_time
1 0
2 0
3 0
4 0
5 0
"user_answers" table:
id qid time
1 1 3
2 1 44
3 2 3.2
4 3 2
5 1 5
6 4 1
I need to finish with "question" table like this:
id total_time
1 52
2 3.2
3 2
4 1
5 0
thanks,

Try this:
UPDATE question
SET total_time = (SELECT SUM(time) FROM user_answers WHERE question.id = user_answers.qid)

Related

How get products id without default 0 if product has attribute

I want to get records from table ps_stock_available without default "0" attribute if product has attribute. As you know in ps_stock_available table show's default attribute as 0
for example ...
id_product id_product_attribute
1 0
1 12
1 13
1 14
2 0
3 0
4 0
4 1
4 2
4 4
What i want is to get records by mysql query such as
id_product id_product_attribute
1 12
1 13
1 14
2 0
3 0
4 1
4 2
4 4
Let me know if someone can help.
on possible way to do this.
select * from tbl_name
where ((p_id in (205, 206) and att_id != 0)
or (att_id = 0 and !(p_id = 205 or p_id=206)));

MySQL: select when count = sum & group by

I need to select user_id & quiz_id, for users which their count of questions in their quiz = sum of correct, this mean they answer 100% correct
answers table:
quiz_id question_id user_id answer_id correct
1 1 1 1 1
1 2 1 6 0
1 3 1 9 1
2 1 2 1 1
2 2 2 5 1
3 4 1 17 1
3 5 1 21 1
3 6 1 25 1
4 1 3 1 1
5 4 4 18 0
6 1 5 1 1
6 2 5 5 1
7 1 3 2 0
7 2 3 7 0
ex 1:
user 1 took "quiz_id" = 1
count of questions in "quiz_id = 1" = 3
sum of correct = 2
so it's not 100%
user_id = 1 in quiz_id = 1 => will not selected
but user_id = 1 will be selected with quiz_id = 3 cause he got 100%
expected results:
quiz_id user_id
2 2
3 1
4 3
6 5
notes:
quiz could be taken with different users with different number of
questions
quiz_id, user_id unique together (user can not take same quiz twice)
thanks,
You should use an aggregate query with HAVING clause:
SELECT quiz_id, user_id
FROM quiz_answer -- or whatever the name is
GROUP BY quiz_id, user_id
HAVING COUNT(question_id) = SUM(correct)
here you must use HAVING instead of WHERE because
The HAVING clause can refer to aggregate functions, which the WHERE
clause cannot
as specified in the docs.

MYSQL Stored procedure, approvals and approvers

Im kinda new reading stored procedure.
I was thinking if this is posible to do in store procedure in mysql.
I have sequence of approval process called step. approved column; 1 is yes 0 is no.
Basically I have Step 1 to 3..in my approval sequence.
if step 1 approved status is 0 he will be the first to approved or see the table.
if step 1 approve is 1. step 2 can now see the table.
Transaction Steps Table:
id transaction_id approver_id step approved
1 1 1 1 1
2 1 2 2 0
3 1 3 3 0
4 2 3 1 1
5 2 1 2 1
6 2 2 3 0
7 3 2 1 0
8 3 3 2 0
9 3 1 3 0
10 4 1 1 1
11 4 3 2 0
12 4 2 3 0
Example If my Approval id = 2
In My View:I can only see all those next in que approvals
id transaction_id approver_id step approved
2 1 2 2 0
6 2 2 3 0
7 3 2 1 0
pls let me know if this is possible. thank you
If I understand correctly, you want rows that are the first non-approved for each transaction and the approver is 2.
Try this:
select ts.*
from transactionsteps ts join
(select transaction_id, min(step) as minstep
from transactionsteps
where approved = 0
group by transaction_id
) t
on ts.transaction_id = t.transaction_id and
ts.step = t.minstep
where approver_id = 2;

MySQL SELECT statement to retrieve field value that is unique

For example
id staff_id skill_id mainskill
1 1 24 1
2 1 24 0
3 1 7 0
4 4 24 0
5 4 18 0
6 6 8 0
7 6 18 1
I would like the result to contain only the tuples with a skill_id that is present only once in all the data. In other words I want to retrieve the tuples containing the skill_ids that are only possessed by a single staff member.
And so the desired output is:
id staff_id skill_id mainskill
3 1 7 0
6 6 8 0
Thanks in advance :).
You can do it with GROUP BY and HAVING, like this:
SELECT
MAX(id) as id,
MAX(staff_id) as staff_id,
skill_id,
MAX(mainskill) as mainskill
FROM MyTable
GROUP BY skill_id
HAVING COUNT(1)=1

How to get the count of rows from a table in cakephp

I have two tables:
id category status
1 test 1
2 test1 1
3 test2 1
This is the group_master table.
groupid groupname groupmaster
1 yy 1
2 xx 1
3 yyyy 1
4 xrx 1
5 yy 2
6 xx 2
7 yyyy 2
8 xfgdrx 3
This is the membergroup table.
The group_master.id is same as in membergroup.groupmaster.That menas i want to get each row from first table and also want to get the count from second table
That means:
id category status Count
1 test 1 4
2 test1 1 3
3 test2 1 1
This is the result i want to get.
How can i do this in Cakephp with pagination ?
try this:
You need to JOIN both tables and do a GROUP BY
SELECT g.id,g.category,g.status,count(*) as Count
FROM group_master g
JOIN membergroup m
ON g.id=m.groupmaster
GROUP BY g.id,g.category,g.status
SQL Fiddle Demo