I want sum values into my database by the same ID in the same table.
Table in database:
| ID | Value_o | Value_t | Value_tt |
| 1 | 40 | 20 | 10 |
query:
SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT
WHERE ID IN(1, 1)
And now the output id:
| Value_o | Value_t | Value_tt |
| 40 | 20 | 10 |
but I want:
| Value_o | Value_t | Value_tt |
| 80 | 40 | 20 |
I want get this output without JOIN.
Thanks!
PS. Sorry for my bad eng :/
Maybe this is what you are looking for:
SELECT
SUM(Value_o) AS Value_o,
SUM(Value_t) AS Value_t,
SUM(Value_tt) AS Value_TT
FROM
(
SELECT ID, Value_o, Value_t, Value_tt FROM Table1
UNION ALL
SELECT ID, Value_o, Value_t, Value_tt FROM Table1
) Table2
WHERE ID IN(1, 1);
Demo
The MySQL in operator doesn't work this way. Even if you have a value multiple times in the set, it doesn't duplicate the rows of your result.
If you want to have all the rows multiple times, you must use union all and sum over that
SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT
from (select * from mytable union all select * from mytable) t
WHERE ID IN (1)
Try this:
SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT
FROM TABLE
GROUP BY ID
HAVING ID = 1
Related
I have a table that contains too many records and each bunch of records belong to someone:
---------------------
id | data | username
---------------------
1 | 10 | ali
2 | 11 | ali
3 | 12 | ali
4 | 20 | omid
5 | 21 | omid
6 | 30 | reza
now I want to create a query to result me like this:
1-10-ali
4-20-omid
6-30-reza
2-11-ali
5-21-omid
3-12-ali
Is there anyway to create a query to result me one record per each username and then one from another, and another to the end?
Unfortunately MySQL doesn't have a ranking system so you can use UDV (user defined variables) to rank your records like so.
SELECT id, `data`, name
FROM
( SELECT
id, `data`, name,
#rank := if(#name = name, #rank + 1, 1) as rank,
#name := name
FROM test
CROSS JOIN (SELECT #rank := 1, #name := '') temp
ORDER BY name, `data`
) t
ORDER BY t.rank, t.name, t.data
Sql Fiddle to play with
Output:
+---------------------+
| id | data | name |
+-----+------+--------+
| 1 | 10 | ali |
+---------------------+
| 4 | 20 | omid |
+---------------------+
| 6 | 30 | reza |
+---------------------+
| 2 | 11 | ali |
+---------------------+
| 5 | 21 | omid |
+---------------------+
| 3 | 12 | ali |
+---------------------+
The classic SQL approach is a self join and grouping that lets you determine a row's ranking position by counting the number of rows that come before it. As this is probably slower I doubt I could talk you out of the proprietary method but I mention it to give you an alternative.
select t.id, min(t.`data`), min(t.username)
from test t inner join test t2
on t2.username = t.username and t2.id <= t.id
group by t.id
order by count(*), min(t.username)
Your example would work with
SELECT id, `data`, name
FROM tbl
ORDER BY `data` % 10,
username
`data`;
If data and username do not have the desired pattern, then improve on the example.
I have a mysql table like this:
+----+------+
| id | rank |
+----+------+
| 1 | 2 |
+----+------+
| 2 | -1 |
+----+------+
| 3 | 5 |
+----+------+
| 4 | 1 |
+----+------+
| 5 | -1 |
+----+------+
| 6 | -1 |
+----+------+
| 7 | 8 |
+----+------+
| 8 | -1 |
+----+------+
Now I want to get the ids in the following order: At first
WHERE rank >= 1 ORDER BY rank ASC
and afterwards:
WHERE rank = -1
How can I get this in only one mysql_query()?
Try something like:
SELECT *
FROM mytable
WHERE rank >= 1
ORDER BY rank
UNION
SELECT *
FROM mytable
WHERE rank = -1
OR something like:
SELECT *
FROM mytable
WHERE rank >= 1
ORDER BY CASE WHEN rank>=1
THEN 0
ELSE 1,rank
Proposed answer:
SELECT id FROM myTable WHERE rank >= 1 ORDER BY rank ASC
UNION
SELECT id FROM myTable WHERE rank = -1
From my understanding, you wanted a column of id's, starting with the ids WHERE rank >= 1 ORDER BY rank ASC and ending with the ids WHERE rank = -1.
The previous sql query uses UNION which joins two resulting tables from separate SELECT queries. UNION can only be applied when you have the same mount of generated columns from each SELECT query, so that's a good thought to keep in mind if later you want to increase the amount of columns obtained.
You can also map rank with ELT.
sample
SELECT *
FROM mytable
ORDER BY ELT(rank+2,99,0,1,2,3,4,5,6,7,8) ASC;
I have table:
+----------------+
| table |
+----------------+
| u_id | sail_id |
+----------------+
| 1 | 5 |
| 1 | 5 |
| 2 | 5 |
| 2 | 4 |
| 1 | 4 |
+----------------+
How to write sql statement to count different u_id with different sail_id (means no duplicate)?
Example:
if SELECT COUNT(*) FROM table GROUP BY sail_id, result will be 2
if SELECT COUNT(*) FROM table GROUP BY sail_id, user_id, result will be 1
I need result to be 4 (because there are 5 rows and only first and second rows have same u_id and sail_id).
Maybe I need add somewhere DISTINCT.
1) You can use COUNT(DISTINCT ...):
SELECT COUNT(DISTINCT u_id,sail_id)
FROM tab;
SqlFiddleDemo
2) You can use subquery with DISTINCT:
SELECT COUNT(*)
FROM (SELECT DISTINCT u_id, sail_id
FROM table) AS sub;
LiveDemo
3) You can use subquery with GROUP BY:
SELECT COUNT(*)
FROM (SELECT u_id, sail_id
FROM table
GROUP BY u_id, sail_id) AS sub;
4) Last possibility is to use:
SELECT COUNT(DISTINCT CONCAT(u_id,',',sail_id))
FROM table;
SqlFiddleDemo
I have a table like
+------+----------+
| id | location |
+------+----------+
| 1 | TVM |
| 2 | KLM |
| 3 | EKM |
+------+----------+
And I have an array of id like [1,2,1,3,1]. I need to get the result as
+------+----------+
| id | location |
+------+----------+
| 1 | TVM |
| 2 | KLM |
| 1 | TVM |
| 3 | EKM |
| 1 | TVM |
+------+----------+
I am already tried WHERE IN like conditions but no luck.
A where statement cannot multiply the number of rows. It can only filter rows out. You want a join:
select tl.*
from tablelike tl join
(select 1 as n union all select 2 union all select 1 union all
select 3 union all select 1
) n
on tl.id = n.n;
Note: if you are already generating the list via a query or from a table, then use that for the query rather than passing the list out of the database and then back in.
You could also return this result with a query like this; this uses a separate SELECT to return each occurrence of row with id=1.
( SELECT id, location FROM mytable WHERE id IN (1,2)
ORDER BY id
)
UNION ALL
( SELECT id, location FROM mytable WHERE id IN (1,3)
ORDER BY id
)
UNION ALL
( SELECT id, location FROM mytable WHERE id IN (1)
ORDER BY id
)
Following a similar pattern, the result could be obtained by combining the results from five SELECT statements, each returning a separate row. That would probably be a little simpler to achieve from a small array, e.g.
$glue = ") ) UNION ALL
(SELECT id, location FROM mytable WHERE id IN (";
$sql = "(SELECT id, location FROM mytable WHERE id IN ("
. implode($glue, $myarray)
. ") )";
I have a mysql table and I need to get random row and get the rank of total view
+--------+------------+---------+
| id | name |totalview|
+--------+------------+---------+
| 1 | ex1 | 20 |
| 2 | ex2 | 100 |
| 3 | ex3 | 30 |
| 4 | ex4 | 40 |
+--------+------------+---------+
for example :
SELECT * FROM `table` WHERE `id` = '$rand';
$rand may be 1 or 2 etc ..
I need to get rank of this row by totalview
thank's
SELECT *,
(SELECT COUNT(*) FROM table t2 WHERE totalview > t1.totalview ) + 1 cnt
FROM table t1
WHERE id = '$rand';
SELECT SUM(ref.totalview < t.totalview) FROM t1 CROSS JOIN t1 ref WHERE t1.id = '$rand'