I have this table
f_id | user_id | sent
==============================
1 | 1 | 0
1 | 2 | 1397820533
1 | 3 | 0
1 | 4 | 1397820533
2 | 1 | 1397820533
2 | 2 | 1397820533
2 | 3 | 1397820533
3 | 1 | 1397820533
3 | 2 | 1397820533
now I would like to have all f_id's where all sent fields have a timestamp ( != 0 )
SELECT * FROM table WHERE sent != 0 GROUP BY f_id
returns all three cause there are some which have a timestamp.
A a result I expect
f_id
=====
2
3
cause f_id still have some 0 values
You are close, but you want to move the condition to the having clause:
SELECT f_id
FROM table
GROUP BY f_id
HAVING SUM(sent = 0) = 0;
The having clause counts the number of rows with sent = 0. The = 0 means there are none of them.
SELECT Distinct f_id
FROM table
WHERE sent >= 0
Try this
SELECT * FROM table
WHERE f_id not in (select f_id from table where sent=0)
GROUP BY f_id
Try this:
select *
from
(SELECT *
FROM table
WHERE sent != 0) tempalias
GROUP BY f_id
Related
I have the following table:
---------------------------------
| id | class_id | time | status |
---------------------------------
| 1 | 1 | <> | 1 |
---------------------------------
| 2 | 2 | <> | 1 |
---------------------------------
| 3 | 1 | <> | 0 |
---------------------------------
| 4 | 2 | <> | 0 |
---------------------------------
I want a query that will see that the first row has class_id = 1 and status = 1. It will then look for the next row with class_id = 1 and status = 0, and find the time difference between the two (time is DATETIME).
At the end of all this, it will return me a sum of all time differences (i.e. (row 1 - row 3) + (row2 - row4)).
How is this possible? In generalisation, the question is about getting an aggregate total of differences between rows in a table, based off a condition.
For every status 0 record we search the latest status 1 record. This is from all previous status 1 records take the latest.
select
class_id,
sum
(
timestampdiff
(
second,
(
select s1.time
from mytable s1
where s1.status = 1
and s1.class_id = s0.class_id
and s1.id < s0.id
order by s1.id desc limit 1
),
s0.time
)
) as diffsecs
from mytable s0
where status = 0
group by class_id;
I have a table with columns ID, Content and Day. I am trying to find all rows that have duplicate Content and Day values and display all rows
SELECT ID,Content, `Day`, Count(*)
FROM table
GROUP BY Content,`Day`
HAVING COUNT(*) > 1
The current code will return a list of duplicate Content and 'Day' values for instance:
ID|Content|Day
1 | a | 1
2 | a | 1
3 | a | 1
4 | b | 2
5 | b | 2
6 | c | 3
7 | c | 4
Will result in:
ID|Content|Day|Count
1 | a | 1 | 3
4 | b | 2 | 2
But I want to display all the unique IDs as well;
ID|Content|Day
1 | a | 1
2 | a | 1
3 | a | 1
4 | b | 2
5 | b | 2
Just make a Sub-Query
select *
from table
where `day` in
(
SELECT ID
FROM table
GROUP BY Content,`Day`
HAVING COUNT(*) > 1
) A
Use that query as a subquery to join against the table again:-
SELECT table.ID, table.Content, table.`Day`
FROM table
INNER JOIN
(
SELECT Content, `Day`, Count(*)
FROM table
GROUP BY Content,`Day`
HAVING COUNT(*) > 1
) sub0
ON sub0.Content = table.Content
AND sub0.`Day` = table.`Day`
I want to search user_id of 1 in table below and get other user_ids related to same mid ( in this case mid of 1 & 3)
mid | user_id
1 1
1 2
1 3
2 2
2 3
2 5
3 1
3 5
3 2
The result must be :
mid | user_id
1 1
1 2
1 3
3 1
3 5
3 2
How is it done with MySQL query ?
Assuming I understood correctly, you want to first find all mid values that have a user_id value of 1, then get all user_id values from all those previously gotten mid values.
SELECT mid, user_id from table
where mid IN (SELECT mid FROM table WHERE user_id = 1)
Assuming that you want to get all rows for those mid that matches a user_id this should do what you want:
select * from your_table t1
where exists (
select 1 from your_table t2
where user_id = 1
and t1.mid = t2.mid
)
Sample SQL Fiddle
Result given your sample data:
| MID | USER_ID |
|-----|---------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 3 | 1 |
| 3 | 5 |
| 3 | 2 |
This is the code i got so far
SELECT users_ID,Problem_ID
FROM 'submission'
WHERE Status = "AC"
GROUP BY users_ID,Problem_ID
I am getting these results
+----------+------------+
| Users_ID | Problem_ID |
+----------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 3 |
+----------+------------+
I only want to get
+----------+------------+
| Users_ID | Problem_ID |
+----------+------------+
| 1 | 3 | -- so because there are 3 results for user_ID 1
| 2 | 2 | -- and there are 2 results for user_ID 2
+----------+------------+
So the Problem_ID is how many rows I am getting from my query for each user.
But how do I accomplish this?
Edit:
I forgot mention that the table contains duplicates of the same problem for example.
I got a Problem with the ID of 1 and then in the database there could be two rows with the same user and with status as "AC" but I want to only get one of them.
SELECT users_ID, count(Problem_ID) as `problem_count`
FROM `submission`
WHERE Status = 'AC'
GROUP BY users_ID;
This should work:
SELECT users_ID, COUNT(DISTINCT Problem_ID)
FROM `submission`
WHERE Status = 'AC'
GROUP BY users_ID
You can do something like this :
SELECT
s.users_ID
,count(s.Problem_ID)+CASE WHEN IFNULL(userDupli.nbrUserAC, 0) > 0 THEN 1 ELSE 0 END as `problem_count`
FROM
`submission` s
left join (SELECT
users_ID
,count(*) as nbrUserAC
FROM `submission`
WHERE Status = 'AC'
GROUP BY users_ID) userDupli
on userDupli.users_ID = s.users_ID
WHERE
Status <> 'AC'
GROUP BY
users_ID
,userDupli.nbrUserAC
I have a table of hits for various users:
HITS:
id | userid
1 | 1
2 | 1
3 | 2
4 | 1
5 | 2
6 | 2
I want the fastest possible way to get a list of these items ranked by ID. So this:
HITS RANKED:
id | userid | ranks
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 1 | 3
5 | 2 | 2
6 | 2 | 3
I want to avoid joining two tables to each other, as this takes forever when the tables get big. Any other suggestions?
SELECT ID,
UserID,
Ranks
FROM
(
SELECT id,
userid,
#group:=CASE WHEN #temp <> userid THEN 1 ELSE #group+1 END AS ranks,
#temp:=userid AS clset
FROM (SELECT #group:= 0) s,
(SELECT #temp:= 0) c,
(SELECT * FROM hits ORDER BY userid, id) t
) x
ORDER BY ID
SQLFiddle Demo