I get stuck in query of sql for this task
If i get all field after contain value 2 and ignore before value 2
For example like this
id profile_id status
1 1 3
2 1 3
3 1 2
4 1 1
5 1 1
and the result when i try like this, show all values where not contain 2
SELECT * FROM `users` WHERE `status` <> 2
id profile_id status
1 1 3
2 1 3
4 1 1
5 1 1
i try code subquery like this
SELECT *
FROM `users`
WHERE `users`.status != (
SELECT us.status
FROM `users` us
WHERE us.status = 2 LIMIT 1)
and the result show like this
id profile_id status
1 1 3
2 1 3
4 1 1
5 1 1
The result should i expected be like this, just show all field after the status = 2
id profile_id status
4 1 1
5 1 1
Thanks for your help
Try below query:
SELECT *
FROM `users`
WHERE id > (
SELECT id
FROM `users`
WHERE status = 2
)
Related
I want to query for each distinct user and their count which logged In as Yes
Userid loggedIn
---- -----
1 Yes
1 yes
1 No
2 Yes
2 No
3 No
4 yes
5 yes
1 No
Output Should be
Userid Count
---- -----
1 2
2 1
3 0
4 1
5 1
with main as (
select distinct Userid from <table>
)
select
main.Userid,
count(*) as total
from main
left join <table_name>
on main.Userid = <table_name>.Userid
and loggedIn = 'yes'
group by main.Userid
or
select
user_id,
sum(case when lower(loggedIn) = 'yes' then 1 end) as count_
from <table_name>
group by 1
id | p_id
--------------------------
1 | 0
2 | 0
3 | 1
4 | 2
5 | 0
6 | 7
7 | 1
8 | 0
This is above table data's, if i give
SELECT * from tablename order by `id` asc
it will bring the above result set
But my use case is i need to sort by both the id and p_id in a different way (i.e) i need to get result set by like below
id | p_id
--------------------------
1 | 0
3 | 1
7 | 1
6 | 7
2 | 0
4 | 2
5 | 0
8 | 0
let me explain briefly about that, p_id value 1 should be the next to the id 1 and that is arranged like above, it clearly show that p_id 1 and p_id 2 is next to the id 1 and 2 respectively and make sure that id 7 in order and id 6 next to id 7
If I understood you correctly , you want the order to be -> If P_id=0 then to order by ID , else, order by p_id.
You can achieve this by conditional ordering using CASE EXPRESSION :
SELECT *
FROM YourTable t
ORDER BY CASE WHEN t.pid = 0
THEN t.id
ELSE t.p_id
END ASC,
t.id
This should return you your expected results.
You need to know the relationship between the id's and since the 2 id blocks have different sort requirements you also need a helper column and a subquery.
create table t (id int, pid int)
insert into t
values
( 1 , 0),
( 2 , 0),
( 3 , 1),
( 4 , 2),
( 5 , 0),
( 6 , 7),
( 7 , 1),
( 8 , 0)
select s.id,s.pid
from
(
select case
when t.id in (1,3,7,6) then 1
else 2
end as ac
,
case
when t.id in (1,3,7,6) then pid
else id
end as sortcol
,
t.*
from t
) s
order by s.ac,s.sortcol
Result
id pid
1 0
3 1
7 1
6 7
2 0
4 2
5 0
8 0
I have these tables
articles_type
id type
1 article
2 free
3 review
user_fav
uid article_type_id
1 1
1 2
2 1
3 2
3 3
4 1
4 2
4 3
I need to back user with article type if user do not added type then display 0
the expected result
for uid = 1
article_type_id count
1 1
2 1
3 0
------------------------------
for uid = 2
article_type_id count
1 1
2 0
3 0
------------------------------
for uid = 3
article_type_id count
1 0
2 1
3 1
------------------------------
for uid = 4
article_type_id count
1 1
2 1
3 1
I have tried the following query to get the expected result.
QUERY:
select article_type_id, count (article_type_id)
from article_types
left join user_fav on user_fav.article_type_id = article_type.id
where uid = 1
When the condition is in the second table of a left join, then you need to include it the on clause. Also, your query needs a group by:
The query you want is:
select a.article_type_id, count(*)
from article_types a left join
user_fav u
on u.article_type_id = a.id and u.uid = 1
group by a.article_type_id;
select article_type_id, coalesce(count(article_type_id),0)
from article_types
left join user_fav on user_fav.article_type_id = article_type.id
where uid = 1
I have a table which looks like this
userid | value | time
1 0 1
1 1 2
1 2 3
3 5 4
4 6 5
1 9 6
3 10 7
Using a select where in query I would want to select userd, value, and time but limit the total number of rows pulled for each userid
My SELECT query,
select userid, value, time from table where userid in (1,3) order by time desc;
This query outputs all the values like so
userid | value | time
1 0 1
1 1 2
1 2 3
3 5 4
3 10 7
I would hover want to limit the number of rows for each userid to two to get an output like so
userid | value | time
1 0 1
1 1 2
3 5 4
3 10 7
I tried using limit but that limits the number of rows for the entire output.
You can use a rank query to limit rows per user
select userid,
value,
`time`
from (
select *,
#r:= case when #g = userid then #r + 1 else 1 end row_num,
#g:=userid
from test t
cross join (select #g:= null,#r:=0) t1
where userid in (1,3)
order by userid,value,`time` desc
) t2
where row_num <= 2
Above query will give rank to each record for same user like user 1 has 3 records the they will assigned rank as 1,2,3 and if user 2 has 2 records then rank will be 1,2 for user 2 and in parent query i am just filtering the records according to the rank that return only result where rank is less than equal to 2 for for each user only 2 rows with rank 1 and 2 will be returned
Demo
table stockholders
stockholder_id election_id user_id
1 1 1
2 1 2
3 2 3
table users
user_id user_type
1 1
2 1
3 1
4 1
5 1
select
*
from
tbl_users
left join tbl_stockholders
on tbl_stockholders.user_id = tbl_users.user_id
where
user_type=1
and stockholders.user_id is null
and election_id <> 1
i want to search election_id not equal to 1 and user type equal to 1
stockholder_id election_id user_id user_type
3 2 3 1
null null 4 1
null null 5 1
this is an update
sorry my problem should be excluding the tbl_stockholders from the tbl_users with the parameter election_id.. because a problem exist when i have a duplicate user_id
table stockholders
stockholder_id election_id user_id
1 1 1
2 1 2
3 2 3
4 1 3
in the previous answer this is the result when election_id<>2
stockholder_id election_id user_id user_type
3 1 3 1
null null 4 1
null null 5 1
this must be
stockholder_id election_id user_id user_type
null null 4 1
null null 5 1
this is my current not working code
select * from tbl_users
where not exists (select * from tbl_stockholders where election_id <> 2)
Try this:
SELECT * FROM users u
LEFT JOIN stockholders s ON u.user_id = s.user_id
WHERE u.user_type = 1 AND (s.election_id <> 1 OR s.election_id IS NULL)
Fiddle here.
I think, this is what you should do:
select
*
from
tbl_stockholders
left join tbl_users
on tbl_users.user_id = tbl_stockholders.user_id
where
(user_type=1
and election_id <> 1)
I don't think you intend to select null user_ids. Also that would invalidate the join i.e you are saying joined the two tables based user_id but select null user_id from the first table.