I have two tables:
table1: id, created
table2: id, modi
+------------+----------+
| id | created |
+------------+----------+
| 1 | 18/01/01 |
| 2 | 18/01/01 |
| 3 | 18/01/01 |
| 4 | 18/01/01 |
+------------+----------+
+------------+----------+
| id | modi |
+------------+----------+
| 1 | 18/01/02 |
| 4 | 18/01/02 |
| 1 | 18/01/03 |
| 2 | 18/01/03 |
| 3 | 18/01/04 |
| 2 | 18/01/04 |
| 2 | 18/01/05 |
+------------+----------+
I need a query (MySQL) that prints out how many days takes every user to modify the log, grouped by days. For example, 1 day - 3 users, 2 day - 7 users, etc...
+------------+----------+
| days | num_id |
+------------+----------+
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
+------------+----------+
I managed to do this:
select datediff(table1.id, table2.modi) as date_diff, count(*) as nums
from table1, table2
where table1.id = table2.id
group by date_diff
The problem is that it is also including the second entry (or more) besides the first one, but I would only want the first one to be included.
Thanks.
you need to use proper column for datediff fucntion
select datediff(table3.modi,table1.created) as date_diff, count(*) as nums
from table1 left join
(
select id,min(modi) as modi from table2 group by id
)
as table3
on table1.id = table3.id
group by date_diff
just add a Join Left in the query. :)
Related
I need to combine information from two tables into one table, the following table is
table 1
+----+---------------+---------+
|id_k| name | value |
+----+---------------+---------+
| 1 | enak | 4 |
| 2 | nor | 3 |
+----+---------------+---------+
table 2
+------+------+---------+
| id_d | id_k | feel |
+------+------+---------+
| 1 | 1 | good |
| 2 | 1 | better |
| 3 | 1 | verygood|
+------+------+---------+
result should be
+------+------+-------+------------------------+
| id_d | name | value | feel |
+------+------+-------+------------------------+
| 1 | enak | 4 | good, better, verygood |
| 2 | nor | 3 | |
+------+------+-------+------------------------+
this is my code [not worked]
select k.name, k.value, s.feel
from table1 as k
left join table2 as s on s.id_k=k.id_k
http://sqlfiddle.com/#!9/3a7564/1
SELECT t1.id_k,
t1.`name`,
t1.`value`,
GROUP_CONCAT(t2.feel) AS feel
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id_k = t2.id_k
GROUP BY t1.id_k
You could use the gorup_concat function to concatinate the values from table2 to a coma-delimited string in the result:
SELECT table1.id_k, name, value, GROUP_CONCAT(feel SEPARATOR ', ') AS feel
FROM table1
JOIN table2 ON table1.id_k = table2.id_k
GROUP BY table1.id_k
I would like to SELECT the records from a table that have different values from another one.
Table 1.
+--------+-------+
| userID | tagID |
+--------+-------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+--------+-------+
Table 2.
+---------+-------+
| ChaName | tagID |
+---------+-------+
| Hello | 1 |
| How | 2 |
| Are | 3 |
| You | 4 |
| Today | 5 |
| Guys | 6 |
| ? | 7 |
+---------+-------+
And then it suppose to be
+--------+-------+---------+
| userID | tagID | chaNAME |
+--------+-------+---------+
| 1 | 1 | Hello |
| 1 | 5 | Today |
| 1 | 6 | Guys |
| 1 | 7 | ? |
+--------+-------+---------+
It looks very simple but I can't find the way to solve it.
Thank you for all of your answers <3
PS. btw i've tried to use ' not in ' but it got an error
Unrecognized keyword. (near "not in" at position 92)
To get the output, you need to use an outer join. A left join will give you all the records from left table table2 and only matching records from right table table1. That means you will get null for records in table1 which doesn't exist in table2. You can use this property in where clause to get only your expected records.
Also you cannot decide on userid on records in table2. If you want to hardcode them as 1, then use select 1 as userid, ... in select clause.
SQLFiddle Demo
select t2.tagid,t2.chaname
from
table2 t2 left join table1 t1
on t1.tagid=t2.tagid
where t1.tagid is null
select a.userid,
b.tagid,
b.chaname
from table1 a,table2 b
where b.tagid not in (select tagid from tabel1)
Here is the structure of my tables:
// table1
+----+-------+--------------------+
| id | color | content |
+----+-------+--------------------+
| 1 | blue | it is a bad color |
| 2 | red | it is a good color |
| 3 | green | I don't like this |
+----+-------+--------------------+
// table2
+----+-------+---------------------+
| id | site | description |
+----+-------+---------------------+
| 1 | stack | help to programmers |
| 2 | google| everything you like |
+----+-------+---------------------+
// votes
+----+-----------+---------+-------+
| id | table_code| post_id | value |
+----+-----------+---------+-------+
| 1 | 1 | 1 | 1 | // table1, post1, +1upvote (blue)
| 2 | 1 | 2 | -1 | // table1, post2, -1downvote (red)
| 3 | 2 | 1 | 1 | // table2, post1, +1upvote (stack)
+----+-----------+---------+-------+
Also here is my query:
select t3.*, (select sum(value) from votes v where t3.id = v.post_id) total_votes
from (
select * from table1
union all
select * from table2
) t3
Here is my output:
+----+-------+---------------------+-------------+
| id | color | content | total_votes |
+----+-------+---------------------+-------------+
| 1 | blue | it is a bad color | 2 | // Problem (it should be 1)
| 2 | red | it is a good color | -1 |
| 3 | green | I don't like this | 0 |
| 1 | stack | help to programmers | 2 | // Problem (it should be 1)
| 2 | google| everything you like | 0 |
+----+-------+---------------------+-------------+
As you see in the this ^ table, the calculation of total_votesis wrong. How can I fix it?
Note: According to reality, I can not combine table1 and table2. So, please don't tell me your structure is crazy.
You have to also specify table_code in the UNION:
select t3.*,
(select sum(value)
from votes v
where t3.id = v.post_id and
v.table_code = t3.table_code) total_votes
from (
select *, 1 table_code from table1
union all
select *, 2 from table2
) t3
Using table_code in the correlated sub-query we can pick the correct values from votes table.
I was having problems in creating counting rows by grouping based on a given field value.
For example: I have a Table A structure like this:
+------+------------+
| id | Person |
+------+------------+
| 1 | "Sandy" |
| 2 | "Piper" |
| 3 | "Candy" |
| 4 | "Pendy" |
+------------+------+
Also I have a Table B structure like this:
+------+------------+---------+
| id | Person | Point |
+------+------------+---------+
| 1 | "Sandy" | 10 |
| 2 | "Piper" | 20 |
| 3 | "Candy" | 30 |
| 4 | "Sandy" | 10 |
| 5 | "Piper" | 20 |
| 6 | "Zafar" | 30 |
+------------+------+---------+
And needed a result like:
+------+------------+---------+
| id | Person | Point |
+------+------------+---------+
| 1 | "Piper" | 40 |
| 2 | "Candy" | 30 |
| 3 | "Zafar" | 30 |
| 4 | "Sandy" | 20 |
| 5 | "Pendy" | 0 |
+------------+------+---------+
I hope the table examples are itself self-explanatory.
SELECT person
, SUM(point) total
FROM
( SELECT person,point FROM table_b
UNION
ALL
SELECT person,0 FROM table_a
) x
GROUP
BY person
ORDER
BY total DESC;
It is a simple left join with a group by
select tableA.person, sum(tableB.points) from tableA left join tableB on tableA.person = tableB.person group by tableA.person
union
select tableB.person, sum(tableB.points) from tableB left join tableA on tableA.person = tableB.person where tableA.id is null group by tableA.person
I think below sql useful to you.
select a.id, a.Person,b.total_point from (
select id, Person from tablea) as a join
(select Person, sum(Point) as total_point from tableb group by person) as b on a.person =b.person
Thank you
I have one table like this:
+----+---------+-------------+
| id | site_id | search_term |
+----+---------+-------------+
| 1 | 2 | apple |
| 2 | 2 | banana |
| 3 | 3 | cheese |
| 4 | 1 | aubergine |
+----+---------+-------------+
And another like this:
+----+---------+-------------+
| id | site_id | search_term |
+----+---------+-------------+
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 1 |
| 5 | 3 | 3 |
| 6 | 1 | 4 |
+----+---------+-------------+
I want to find out how many times each search_term shows up in the 2nd table, and how many times.
In other words, from this data, if I was asking about site_id 2, I'd want this to be returned:
+-------------+-------+
| search_term | count |
+-------------+-------+
| apple | 3 |
| banana | 1 |
+-------------+-------+
I'm familiar with basic joins and such, as well as COUNT, but I'm not sure how to count things from another table.
Thanks!
You could join the tables together, and count the number of rows in the second table:
select t1.search_term
, count(t2.id)
from table1 t1
left join table2 t2
on t1.id = t2.search_term
group by t1.search_term
Give this a try:
select t1.search_term, count(*) from t1
join t2 on t1.id = t2.search_term and t1.site_id = t2.site_id
where t1.site_id = 2
group by t1.search_term