i have the following table
id
name
flag
1
carl
0
2
mark
1
3
steve
1
what I want to do is the first row would override the succeeding row value for flag
so the result would be something like this
id
name
new_inherited_flag
original_flag
1
carl
0
0
2
mark
0
1
3
steve
0
1
how do i achieve this?
You could just use a subquery to find the earliest flag value:
SELECT id, name,
(SELECT flag FROM yourTable ORDER BY id LIMIT 1) AS new_inherited_flag,
flag AS original_flag
FROM yourTable
ORDER BY id;
Related
I have orders table like this:
OID CID ODATE
1 1 01/01/21
2 2 01/02/21
3 2 20/01/21
4. 3. 20/01/21
5. 4. 20/01/21
I want to see all the orders of cid 2 first then all the others
thanks ...
You may order using a CASE expression:
SELECT *
FROM yourTable
ORDER BY CASE CID WHEN 2 THEN 1 ELSE 2 END;
You may also add more sorting levels after the above CASE expression.
I'm saving person image in one table, one person will have multiple images, each image will have status 1 or 0, we need to get the record based on image status. if the image status of a person record is all 1's then we need to consider that record if any one of the status is 0 then we need to skip the record from the result
Original Result
id psqno status
1 1 1
2 1 1
3 1 0
4 2 1
5 2 1
6 3 1
7 3 0
8 4 1
9 4 1
Expected Result
id psqno status
1 2 1
2 4 1
I have tried the query like below:
select count(distinct psqno) as image_record from users where status = 1
I would like to get the count of result whose image having all status as 1.
You can use conditional aggregation to find groups for which no other status exists:
SELECT psqno
FROM t
GROUP BY psqno
HAVING COUNT(CASE WHEN status = 1 THEN 1 END) = COUNT(*) -- all rows have status = 1
Demo on db<>fiddle
I'm looking to have one mySQL query where I can select all records set to a particular value, then selecting a number of other records from the records left, in a random order.
To explain, here's an example: (MyTable database table)
ID Name SomeValue
1 Fred 0
2 Jake 0
3 Jone 1
4 Bill 0
5 Greg 0
6 Thom 2
7 Jane 3
8 Erin 0
So first, I'd want to select all records where SomeValue is more than 0. The appropriate mySQL query for this would be:
SELECT * FROM MyTable WHERE SomeValue > 0 ORDER BY SomeValue DESC
This would return:
7 Jane 3
6 Thom 2
3 Jone 1
Next (in the same query), how would it be possible to select 3 other records at random (limit 6), from the ones remaining?
So that the end result of records returned looks something like this:
7 Jane 3
6 Thom 2
3 Jone 1
5 Greg 0 // Randomly picked
1 Fred 0 // Randomly picked
8 Erin 0 // Randomly picked
Use union all:
(select t.*
from t
where somevalue > 0
) union all
(select t.*
from t
where somevalue = 0
order by rand()
limit 3
);
If you want the values ordered by somevalue add order by somevalue as the last line of the query.
I've a table like below which holds user records. I want to list the users whose permission field is 0 first and then the users with permission 1. But I also want to sort them alphabetically.
This is my table:
users
--------------------------------
user_id name permission
1 jack 0
2 anne 0
3 kate 0
4 steve 1
5 roger 0
6 judy 1
7 robin 0
8 stella 1
9 arthur 0
And I want to get the this result:
users
---------------------------------
user_id name permission
2 anne 0
9 arthur 0
1 jack 0
3 kate 0
7 robin 0
5 roger 0
6 judy 1
8 stella 1
4 steve 1
As you can see, there are two groups, first the users with permission "0" and later permission "1". But each group is also sorted alphabetically in itself.
(
SELECT *
FROM `users`
ORDER BY name ASC
)
UNION (
SELECT *
FROM `users`
ORDER BY name ASC
)
ORDER BY permission ASC
I've tried to get the two groups separately by sorting and combine them but it is not working in the way I want.
A code like below works
SELECT *
FROM `users`
ORDER BY permission ASC , username ASC
you should write the name of the column that you want to initially order by first, then the second, third, etc
SELECT
*
FROM users
ORDER BY
permission ASC,
name ASC;
You don't need two separate queries
First the result set is sorted in ascending order (from lower to higher) based on the value of permission column.
Now you have a sorted list where all the records having 0 as permission will appear first.
Next, if you apply another sort on name column on this sorted list then it will make an alphabetical sorted list where the previous sorting order holds as well as this one.
Test:
create table users(
id int primary key AUTO_INCREMENT,
name varchar(50),
permission int
);
INSERT INTO users(name,permission)
VALUES ('A',1),('D',0),('C',0),('B',1);
SELECT * FROM users;
id name permission
1 A 1
2 D 0
3 C 0
4 B 1
#Sort only by permission (ASC)
SELECT * FROM users ORDER BY permission ASC;
id name permission
2 D 0
3 C 0
1 A 1
4 B 1
#Sort first by permission (ASC), then by name (ASC)
SELECT * FROM users ORDER BY permission ASC, name ASC;
id name permission
3 C 0
2 D 0
1 A 1
4 B 1
SEE DEMO
You can just sort by both fields. No need to do whatsoever.
SELECT
*
FROM yourtable
ORDER BY name ASC,
permission ASC
I have table:
ID Name label
1 Jan 1
2 Jan 2
3 Adam 2
4 Adam 10
5 Kasia 1
I would like get records only with label 1 and label 2
For example:
1 Jan
I have many labels, so subselect in subselect in subselect... is it bad idea
You can do this in various ways. My preferred method is group by and having:
select min(id), name
from table
group by name
having sum(case when label = 1 then 1 else 0 end) > 0 and
sum(case when label = 2 then 1 else 0 end) > 0 ;
For each name, this counts the number of times that "1" appears and that "2" appears. The > 0 ensure that each appears at least once.
One other approach is to use a intersect operator.
select id, name
from tablename where label = 1
intersect
select id, name
from tablename where label = 2