I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..
Related
I have the following table structure:
name
value
success
name 1
10
0
name 2
20
0
name 2
30
1
And my query is:
SELECT name, SUM(value) as valueTotal FROM TableName GROUP BY name
The result is:
name
valueTotal
name 1
10
name 2
50
Now I want to add a new column which will contain the sum of only successful rows. But if I add this condition, it will apply to all selected fields:
SELECT name, SUM(value) as valueTotal, SUM(value) as successValueTotal FROM TableName WHERE success = 1 GROUP BY name
Here is the result I want to get:
name
valueTotal
successValueTotal
name 1
10
0
name 2
50
30
How can I add a field with a separate condition that does not affect the main query? Thx)
You can use the SUM function with a conditional aggregation on whether success is 1 or not. When success is 1, then take the value of the value field, otherwise sum up 0.
SELECT name,
SUM(value) AS valueTotal,
SUM(IF(success = 1, value, 0)) AS successValueTotal
FROM TableName
GROUP BY name
Try it here.
This is the typical use case for CASE WHEN:
SELECT name,
SUM(value) AS valueTotal,
SUM(CASE WHEN success = 1 THEN value ELSE 0 END) AS successValueTotal
FROM TableName
GROUP BY name
You can (like lemon showed) also use an if clause in MYSQL. This is a bit shorter, but the query will not work on every DB while CASE WHEN does. So I think both is fine.
When I use in keyword in sql, there may be some id is missing , but I want treat them like they exist and other columns are null or 0.
For example, suppose I have a table with two columns and some rows:
[id,value1]
1 1
2 4
3 3
5 5
I may write sql like this:
select * from table where id in (1,4,5) order by value1 limit 0,2 ;
When this sql is executed, the return result is [(1,1),(5,5)].
But what I want is [(4,0),(1,1)], because I want to treat the missing id 4 like it exists in the table.
So the question is : Is there some elegant way to achieve it using sql instead of select all rows and sort them in memory.
Use a left join:
select *
from (select 1 as id union all
select 4 union all
select 5
) i left join
table t
using (id)
order by t.value1
limit 0, 2 ;
Note that you are ordering by a value in the existing table, so this depends on the fact that NULL is ordered before other values.
iam having table like this
id name sol_id
1 abc 2,5,8
2 dt 5,9,10
here i want to add some value(10) to id=1 of sol_id,so value 10 will be added with id=1 and at the same time value 10 of id=2 replace with some empty value i want output like this
id name sol_id
1 abc 2,5,8,10(here updating)
2 dt 5,9 (10 removing)
i wrote query like this but its performs one operation not both
UPDATE my_table SET sol_id=REPLACE(sol_id,',10,',',')
and sol_id = Concat(sol_id, ',', 10) where id = 1
is it possible? Thanks in advance
It is possible, but clunky.
Basically you would do
UPDATE table SET SET field = CASE id
WHEN 1 THEN <formula for the case of id=1>
WHEN 2 THEN <formula for the case of id=2>
END
WHERE ID IN (1, 2);
There's no great advantage over running several queries inside a TRANSACTION and, if necessary, a suitable lock on the table.
I have a table name current_record in mysql database.
id num
1 0
2 1
3 1
4 1
5 0
my question is : how find the 1st id of num = 1 from last means down to up or 3 times up
the output should return like this
id = 2 num = 1
please write a sql query.
You can use MIN() combined with GROUP BY to achieve this. Something like this:
SELECT MIN(id) AS id, num FROM current_record GROUP BY num
Optionally you can add a WHERE clause if you specifically want just one value instead of one for each num:
WHERE num == 1
I have a table like this:
Tb_server
ID
5
6
7
Tb_upload
ID
1
2
3
I need a query which can update all the ID of Tb_server to max(Tb_upload.ID) + 1
So, the result on the Tb_server should be like this
Tb_server
ID
4
5
6
I am doing this in a shell script, so I can get the max(Tb_upload.ID) as a variable.
But what will be the query, using MySQL?
Try this:
UPDATE Tb_server, (SELECT #auto:=0) a
SET ID = (SELECT MAX(ID) FROM Tb_upload) + (#auto:= #auto+1);
Take the max+1 of Tb_upload which is : 4
Make the update as :
update Tb_server set Tb_serverID= (Tb_serverID+4); // 4 is the difference here