I want to concat the total of my sum function, please help me, Thanks
This is my query:
SELECT t1.province, t2.fullname,sum(t1.total_vote) AS total
FROM votes AS t1, candidate AS t2
WHERE t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
So just use concat. (And an inner join would be neater.)
SELECT concat(t2.fullname, ' has ', sum(t1.total_vote), ' votes in ', t1.province, '.') AS concatinated_line
FROM votes AS t1
INNER JOIN candidate AS t2 ON t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
The above answer should be correct, just add a closing parens in the SELECT:
SELECT concat(t2.fullname, ' has ', sum(t1.total_vote), ' votes in ', t1.province, '.') AS concatinated_line
FROM votes AS t1
INNER JOIN candidate AS t2 ON t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
Try this:
SELECT fullname , GROUP_CONCAT(cast(total as char))
FROM
(
SELECT t2.fullname fullname,sum(t1.total_vote) AS total
FROM votes AS t1, candidate AS t2
WHERE t1.candidate_id = t2.id
GROUP BY t2.id, t1.province
ORDER BY t2.id
) s
Related
With the syntax below, t3(table3) columns are not displayed but I need to get the page_ID from the table.. Any help?
SELECT t1.*,
GROUP_CONCAT(t2.page_num separator ', ')pageNR
FROM t1
JOIN t2 ON t2.pid = t1.pid
JOIN t3 on t3.ID = t1.ID
WHERE t1.ID = ?
AND t3.page_num = t2.page_num
ORDER BY t1.tag
Presumably, something like this:
SELECT t1.*,
GROUP_CONCAT(t2.page_num separator ', ') as pageNR,
GROUP_CONCAT(t3.page_id separator ', ') as pageIds
FROM t1 JOIN
t2
ON t2.pid = t1.pid JOIN
t3
On t3.ID = t1.ID AND t3.page_num = t2.page_num
WHERE t1.ID = ?
GROUP BY t1.ID;
I have a complex mysql query where one of the Select fields is Min(value). Since all the 'values' are unique, is there also a way to get found min value's row id along?
In other words if we simplify the query to this question, it is like this:
SELECT t1.name, MIN(t2.value) AS minval
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY id_user
How can i now know which t2.id was chosen for lowest t2.value for particular user? Thank you!
Use ROW_NUMBER() to find the first value of each id_user
You can replace * with the fields you need
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY t2.id_user ORDER BY t2.value) as rnk
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
) as X
WHERE X.rnk = 1
Maybe this simple, dont know how complex your statement is:
SELECT name,value,id
FROM(
SELECT t1.name,t2.value,t2.id
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY t2.id,id_user
ORDER BY t1.name,t2.id asc) as test
GROUP BY name;
I have got this query, which works fine:
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
ORDER BY t1.timestamp DESC
LIMIT 1000
What I would like to do is to:
1) get only those entries where the same ip occurs at least 3 times
2) group the entries by ip
So the result would look like this example:
IP TIMESTAMP
111.111.111.111 1500000000
111.111.111.111 1300000000
111.111.111.111 1100000000
222.222.222.222 1400000000
222.222.222.222 1300000000
222.222.222.222 1200000000
I have tried many approaches and I believe that this one is the closest,
but the result is 0 rows.
SELECT *, COUNT(DISTINCT ip) FROM (
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
ORDER BY t1.timestamp DESC
LIMIT 1000
) AS tmp_table
GROUP BY ip
HAVING COUNT(DISTINCT ip) > 2
Please can someone shine some light on this?
Try this:
SELECT t1.*, (SELECT DISTINCT t2.ip FROM t2 WHERE t2.id = t1.t2id)
FROM t1
WHERE
(SELECT COUNT(*)
FROM t2
WHERE t2.id = t1.t2id) >= 3
Bacause in the comments has resulted table t2 with more rows for the same IP I change my query as follow:
SELECT t1.*, t2.ip
FROM t1
JOIN t2
ON t2.id = t1.t2id
WHERE
(SELECT COUNT(*)
FROM t2 tt2
WHERE t2.ip = tt2.ip) >= 3
You can see SqlFiddle
SELECT *, COUNT(ip) FROM (
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
ORDER BY t1.timestamp DESC
LIMIT 1000
) AS tmp_table
GROUP BY ip
HAVING COUNT(ip) > 2
just remove distinct
You have to have the HAVING in the subquery
SELECT t1.*, t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
INNER JOIN
(
SELECT t2.ip
FROM table1 t1
INNER JOIN table1 t2 ON ( t2.id = t2.t2id )
GROUP BY t2.ip
HAVING count(ip) > 2
) t ON t2.ip = t.ip
Try like this;
SELECT t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
where t2.ip IN (SELECT ip FROM (
SELECT t2.ip as ip
FROM table1 t1
INNER JOIN table2 t2 ON ( t2.id = t1.t2id )
) AS tmp_table
GROUP BY ip
HAVING COUNT(*) > 2)
ORDER BY t1.timestamp DESC
LIMIT 1000
I have a query:
SELECT
t1.name as Name
count ( distinct t2.key ) as Total
SUM ( IF( t2.time = '12:00' , 1 , 0) ) as QttMidDay
FROM t1
LEFT JOIN t2 on t1.key = t2.key
GROUP BY t1.key
The question is, how i do the "Conditional Count" on the 2ยบ parameter SUM for QttMidDay ?
I am guessing that you can solve your problem by aggregating before the join. My best guess is:
SELECT t1.name as Name, t2.Total, t2.QttMidDay
FROM t1 LEFT JOIN
(SELECT COUNT(DISTINCT t2.key) as Total,
SUM(t2.time = '12:00') as QttMidDay
FROM t2
GROUP BY t2.key
) t2
ON t1.key = t2.key;
I am not sure if the COUNT(DISTINCT) is necessary in the subquery.
I have the following SQL query:
SELECT t1.id as id,
t1.username as username,
CONCAT_WS(' ', t2.ad,t2.soyad) as fullname,
t2.title as title,
t3.pass as password,
t3.phone_login as hat,
t2.time as date
FROM kullanici t1, kullanici_tanim t2, dial_users t3
WHERE t1.id = t2.usr_id AND t1.agent_id = t3.user_id
GROUP BY t1.id
ORDER BY t1.id ASC
This query works fine. What I'm wondering is should I use joins? What is the correct way?
For those who will have to use your code, please use joins. E.g.:
SELECT t1.id as id,
t1.username as username,
CONCAT_WS(' ', t2.ad,t2.soyad) as fullname,
t2.title as title,
t3.pass as password,
t3.phone_login as hat,
t2.time as date
FROM kullanici t1
INNER JOIN kullanici_tanim t2
ON t1.id = t2.usr_id
INNER JOIN dial_users t3
ON t1.agent_id = t3.user_id
GROUP BY t1.id
ORDER BY t1.id ASC