Get rows with same primary key with different status in reference table - mysql

I'm having following table
id task_id stage_id is_completed
1 1 1 1
2 1 2 1
3 1 3 0
4 2 1 1
5 2 2 0
6 2 3 1
7 3 1 1
8 3 2 1
9 3 3 0
How can I get all task_ids where stage_id = 1 and is_completed = 1 and stage_id = 2 and is_completed = 0

You can use the combinations of AND and OR, e.g.:
SELECT task_id
FROM tasks
WHERE (stage_id = 1 AND is_completed = 1)
OR (stage_id = 2 AND is_completed = 2)
GROUP BY task_id;

SELECT * from mt_task_status
where
task_id in ( SELECT task_id FROM mt_task_status WHERE stage_id = '1' and is_completed = '1')
and task_id in ( SELECT task_id FROM mt_task_status WHERE stage_id = '2' and is_completed = '0')
and stage_id in (1,2)
order by task_id

Use union:
select task_id from table where stage_id = 1 and is_completed = 1
union
select task_id from table where stage_id = 2 and is_completed = 0

Related

Mysql order column and not null

My sql table looks like
ID C1 R1 R2
1 1 1 1
2 1 1 NULL
3 1 1 2
4 1 1 3
5 1 2 NULL
6 1 2 4
I want to retrieve the rows with C1 ID of 1 and after that sort by R1 ascending and first get the NULL value and after that ascending R2.
So my results would be:
ID C1 R1 R2
1 1 1 NULL
2 1 1 1
3 1 1 2
4 1 1 3
5 1 2 NULL
6 1 2 4
Select * from Table t where t.C1 = 1 ......
How can I make my sql query to do what I want?
Below query will work:
select * from table_name where C1=1 order by R1, R2 ASC
select * from table1 where c1=1 order by r1, r2

Show latest ID and company ID based on job ID

Table: service
id companyid jobid
1 1 1
2 1 2
3 1 2
4 2 3
5 3 4
6 3 1
7 4 2
8 5 2
I type the following query:
SELECT *
FROM service
WHERE jobID = 2
ORDER BY companyID desc, ID desc
And I get the below output:
id companyid jobid
8 5 2
7 4 2
3 1 2
2 1 2
But I want my expected output is below:
id companyid jobid
8 5 2
7 4 2
3 1 2
How do I modify the query in order to get my expected output?
You should be able to just apply the max function along with a group by clause:
select max(id) id, companyid, jobid
from service
where jobid = 2
group by companyid, jobid
order by id desc
Given your sample data the result would be:
id companyid jobid
8 5 2
7 4 2
3 1 2
This should work:
select * from service s1
join (select max(id) as id
from service
group by jobid, companyid) s2 on s1.id = s2.id
where s1.jobid = 2--can comment to select all latest jobs

MySQL group-by with comma-seperated list

I have following data
id category_id bookmark_id
1 1 1
2 2 1
3 3 1
4 4 2
5 5 1
and mysql query is
SELECT * from table_name group by bookmark_id
it returns
id category_id bookmark_id
1 1 1
4 4 2
which is fine
But i want one more colum extra
id category_id category_list bookmark_id
1 1 1,2,3,5 1
4 4 4 2
Any idea how to achieve above result?
select min(category_id) as min_cat,
group_concat(distinct category_id order by category_id) as category_list,
bookmark_id
from your_table
group by bookmark_id

How to Update a Field From The Same Data in MySQL

id product main_image
--- ---------- -----------
1 1 0
2 1 0
3 1 0
4 2 0
5 2 0
6 2 0
7 3 0
8 3 0
9 3 0
10 3 0
I want to use mysql query to make table field datas (for main_image) like this
id product main_image
--- ---------- -----------
1 1 1
2 1 0
3 1 0
4 2 1
5 2 0
6 2 0
7 3 1
8 3 0
9 3 0
10 3 0
how can I do it? I want to set a main image for products.
I have about 3 millions record, I tried php but its too slow
You can use a subquery to select the smaller id form the table:
UPDATE myTable
SET main_image = 1
WHERE id IN
(SELECT MIN(temp.id)
FROM (SELECT * FROM myTable) AS temp
GROUP BY temp.product)
This...
UPDATE my_table x
JOIN
( SELECT product
, MIN(id) min_id
FROM my_table
GROUP
BY product
) y
ON y.product = x.product
AND y.min_id = x.id
SET x.main_image = 1;
... or just this ...
UPDATE my_table x
JOIN
( SELECT MIN(id) min_id
FROM my_table
GROUP
BY product
) y
ON y.min_id = x.id
SET x.main_image = 1;

How do I count total of value in one column with different data

I'm a bit confuse how to name my question title.
Let say my MySQL database table like this. table_group
UserID UserGroup UserStatus
------------------------------
1 1 1
2 1 1
3 1 2
4 2 3
5 3 2
6 3 1
7 4 3
9 4 4
10 4 1
I want to group it by UserGroup and count the UserStatus.
Let me know what is the correct statement to get the result like this
UserGroup Status_1 Status_2 Status_3 Status_4
-------------------------------------------------
1 2 2 0 0
2 0 0 1 0
3 1 1 0 0
4 1 0 1 1
SELECT UserGroup, count(UserStatus = 1) as Status_1, count(UserStatus = 2) as Status_2,
count(UserStatus = 3) as Status_3, count(UserStatus = 4) as Status_4
FROM table_group GROUP BY UserGroup
You could use case to count rows that match a condition:
select UserGroup
, count(case when UserStatus = 1 then 1 end) as Status_1
, count(case when UserStatus = 2 then 1 end) as Status_2
, count(case when UserStatus = 3 then 1 end) as Status_3
, count(case when UserStatus = 4 then 1 end) as Status_4
from table_group
group by
UserGroup