I have a table called items_status which has 3 fields, item_id, user_id, and status, which can be either 'have' or 'want'.
Field Type Null Key
user_id varchar(10) NO PRI
item_id varchar(10) NO PRI
status set('have','want') YES NULL
I have a page where I want to get a list of all the user ids in the table ordered by the number of records their user id is associated with in the table where status is 'have'. So far, this is the best I can come up with:
SELECT user_id
FROM items_status AS is
ORDER BY
//Subquery to get number of items had by user
(SELECT COUNT(i.item_id)
FROM items_status AS i
WHERE i.user_id = is.user_id AND i.status = 'have') DESC
GROUP BY user_id
However, this pulls up an error on the subquery. How can I get all of the user ids in the table ordered by the number of items they have?
you can do it like this:
SELECT user_id
FROM items_status
WHERE `status` = 'have'
GROUP BY userID
ORDER BY COUNT(user_id) DESC
SQLFiddle Demo
with slight difference of column name but the query is the same
SELECT user_id, SUM(CASE WHEN i.status = 'have' THEN 1 ELSE 0) AS s
FROM items_status AS is
GROUP BY user_id
ORDER BY SUM(CASE WHEN i.status = 'have' THEN 1 ELSE 0) DESC
Related
Select country, price, cost
from table1
where user_id = 1 and is_enabled = 1 and country = 'IN' and sender_id = 'TEXT'
The above query will return the records if where conditions are met.
Now, I want query to return records even if the conditions are not met but by removing the sender_id from the where condition i.e
Select country, price, cost
from table1
where user_id = 1 and is_enabled = 1 and country = 'IN'
Please help!
I tried using CASE When but didn't achieved the result.
This should help if you need records anyways.
SELECT country, price, cost FROM table1
WHERE user_id = 1 AND is_enabled = 1 AND country = 'IN' OR NOT sender_id = 'TEXT'
In another use case, you can get the sender_id column if matched by using the below query but it will give you null for all unmatched records.
SELECT country, price, cost, CASE WHEN sender_id <> 'TEXT' THEN sender_id ELSE NULL END AS sender_id
FROM table1 WHERE user_id = 1 AND is_enabled = 1 AND country = 'IN'
I am using Alias.
Below is my query. I want to avoid showing rows with NULL counter.
SELECT activity_id, user_id,
(CASE WHEN activity_id = 1 OR activity_id = 2 THEN user_id END) AS counter
FROM eventedge_ticket_activity
It gives the following result:
How can I skip rows with counter NULL?
Not tested, but below should work.
SELECT activity_id, user_id,
CASE activity_id WHEN 1 THEN user_id
WHEN 2 THEN user_id
ELSE NULL
END AS counter
FROM eventedge_ticket_activity
WHERE counter IS NOT NULL
Below is the query which ran successfully.
SELECT * FROM (SELECT activity_id, user_id, case when activity_id=1 then user_id when activity_id=2 then user_id end as counter FROM eventedge_ticket_activity) db where counter != 'NULL'
I have a feeling that the sample data is over simplified so I may be quite wrong but this looks like the same query parsing the data only once.
SELECT activity_id, user_id,
user_id AS counter
FROM eventedge_ticket_activity
WHERE activity_id in(1,2)
BTW the answer provided by marmeladze should also work with the change I mentioned in my comment
Inner 'SELECT' gives table with NULL's, outer 'SELECT' cuts off rows with NULL counter
SELECT * FROM (
SELECT activity_id, user_id,
(CASE WHEN activity_id = 1 OR activity_id = 2 THEN user_id END) AS counter
FROM eventedge_ticket_activity) AS ResultTable
WHERE ResultTable.counter IS NOT NULL
I am making a query in which i want the job ids to be grouped but i want the latest timestamp row in the result which is not happening
Here is the SQL fiddle
http://sqlfiddle.com/#!9/de8769
The normal view for table is
The output after using this query i made
SELECT
DISTINCT(user_id),
job_id,
message,
receiver_id,
parent,
type,
id as id FROM ai_ms_messages
WHERE (receiver_id = '7' OR user_id = '7') AND type<>0 AND type<>2 group by job_id
ORDER BY max(timestamp) DESC
But as you can see its taking the value of id as 3 for job_id 11 but it should have taken the value 5 (as that is latest for job_id 11) and also the order is wrong. Since job_id 11 is latest not job_id 12. Is there any way to achieve this ?
The query would be:
select
distinct(m1.user_id),
m1.job_id,
m1.message,
m1.receiver_id,
m1.parent,
m1.type,
m1.id as id from ai_ms_messages as m1
where m1.type<>0 and m1.type<>2
and m1.timestampt = (select max(m2.timestamp) from ai_ms_messages as m2 where m2.job_id = m1.job_id)
As per your query you are looking for data for receiver_id = '7' and for id =5 , receiver_id = '6' , so this is not in your query output.
Just remove where condition, or check data as per condition only.
GROUP BY groups on the first matching result it hits.
So, its preferable this method as the subquery.
SELECT *
FROM (
SELECT DISTINCT (
user_id
), job_id, message, receiver_id, parent,
TYPE , id AS id
FROM ai_ms_messages
WHERE (
receiver_id = '7'
OR user_id = '7'
)
AND TYPE <>0
AND TYPE <>2
ORDER BY TIMESTAMP DESC
) AS sub
GROUP BY job_id
I'm trying to figure out how I can query my table to see if a group of user_id's match a conversation_id.
Query 1 should return result for:
user_id 1 is looking to see if there are any conversation_id's with just user_id = 2 and user_id = 1 in it. (Should return a row for each conversation_id = 1, 2, 4, 5 based on SQL Fiddle example)
conversation_id
1
2
4
5
Query 2 should return result for:
user_id 1 is looking to see if there are any conversation_id's with user_id = 2, user_id = 1, and user_id = 4 in it. (Should return 0 rows as it doesn't exist in the SQL Fiddle example)
The table setup is located at
SQL Fiddle
You can use a combination of group by ... having and a correlated exists subquery to achieve the result you want:
-- Query 1:
SELECT
conversation_id
FROM
users_conversations uc
where not exists (
select 1 from users_conversations
where conversation_id = uc.conversation_id
and user_id not in (1,2)
)
group by conversation_id
having count(distinct user_id) = 2;
-- Query 2: same query, only different numbers.
SELECT
conversation_id
FROM
users_conversations uc
where not exists (
select 1 from users_conversations
where conversation_id = uc.conversation_id
and user_id not in (1,2,4))
group by conversation_id
having count(distinct user_id) = 3;
Sample SQL Fiddle
Note that the first query will not return 1,2,4,5 but rather 2,5 but in your sample data neither 1 or 4 has only user_id 1 and 2 as participants (conversation 1 has 1,2,3,4, and conversation 4 has 1,2,5).
If i understand it right it should be something like his.
Q1:
SELECT
CASE
WHEN
count(distinct CASE WHEN user_id in ('1','2') THEN user_id END)>='2'
THEN `conversation_id`
END 'test'
FROM
users_conversations
where 1
group by `conversation_id`
Q2:
SELECT
CASE
WHEN
count(distinct CASE WHEN user_id in ('1','2','4') THEN user_id END)>='3'
THEN `conversation_id`
END 'test'
FROM
users_conversations
where 1
group by `conversation_id`
http://sqlfiddle.com/#!9/fb29d/9
When I'm trying to run this query:
select * FROM `activity`
WHERE user_id = 1
AND activity_id NOT LIKE (select activity_id from activity where user_id = 1 ORDER BY activity_id DESC LIMIT 8)
I get the follow error:
Subquery returns more than 1 row
How can I solve this problem? I want to select the activity_id from the table excluding the latest 8 activity_id's for a certain user.
NOT LIKE is expecting an expression or a value to compare against and not a resultset.
Change NOT LIKE for NOT IN
Try this one:
SELECT * FROM `activity`
WHERE user_id = 1 AND activity_id NOT IN (
SELECT activity_id FROM activity WHERE user_id = 1
ORDER BY activity_id DESC LIMIT 8)
Solved it by doing this:
$sql2 = "DELETE t1.*
FROM activity t1
left join (select activity_id from activity where user_id = '".$row['user_id']."' ORDER BY activity_id DESC LIMIT 8) t2
on (t1.activity_id = t2.activity_id)
where t2.activity_id is null
and t1.user_id = '".$row['user_id']."'";