Substring in mysql. Selecting some data - mysql

I have a table like
ID Value
A2424 1
A5355 2
A6363 3
A4634 4
AA_A2424 5
AA_A6363 6
I would like to select only those ID's that show up after AA_
so my output should be
ID Value
AA2424 1
AA5355 2
I tried this but it isn't giving me the right output
SELECT *
FROM table
WHERE ID LIKE SUBSTRING( 'AA_', 4, 8 )
Can anyout suggest??
Thanks

Try:
SELECT ID
, Value
FROM table
WHERE SUBSTRING(ID, 1, 3) LIKE 'AA_%'

I guess your output isn't correct for this example table. It should be:
ID Value
A2424 1
A6363 3
Here is a query to get what you need:
select * from AATable where id in
(select substring(id,4,100) from AATable where id like 'AA_%')

Related

Is it possible to do "reverse" prefix search

I have a table with these columns:
id
name
1
a
2
a.b
3
a.b.c
4
a.b.c.d
5
d
6
d.e
7
d.e.f
If I run query:
SELECT id FROM table WHERE name LIKE 'a%'
I can get 1, 2, 3 and 4. However, is it possible to do the reverse, such as:
SELECT id FROM table WHERE prefix_match(name, 'a.b.c')
which will return 1, 2, 3, but not 4.
Does MySQL have such prefix_match logic?
Try this:
SELECT id FROM table WHERE 'a.b.c' LIKE concat(name, '%')
See it work here:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=484dc7c4acee09de5129c4ebe1b47edf
SELECT id FROM `names` WHERE SUBSTR('a.b.c', 1, LENGTH(name)) = name
This will select all the IDs whose corresponding name begins with a substring of 'a.b.c', but only for names that are not longer than 'a.b.c' (5 chars).

How to treat missing id 's value as 0 and order by it?

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.

What will be Mysql query for group by and order by dictionary style?

Trying to fetch data and print as like dictionary.
Table:
blog_tags
id name
1 atag1
2 atag2
3 dtag1
4 etag1
5 etag2
6 ctag1
7 ctag2
8 ctag3
9 ztag1
I want the data output as:
A
atag1
atag2
C
ctag1
ctag2
D
dtag1
E
etag1
etag2
Z
ztag1
Started with this:
select name from blog_tags order by name;
what will be mysql query for this?
Try something like this
select name
from (
select distinct upper(substring(name, 1, 1)) as name
from blog_tags
union all
select name
from blog_tags
)
order by name
Edit
If you want to get raw data for application level manipulation, I would suggest querying the db this way
select upper(substring(name, 1, 1)) as key,
name
from blog_tags
order by 1, 2
You can use the bellow query to achieve this as given below
select substr(name,1,1),group_conact(name) from blog_tags group by substr(name,1,1);
This query will group the name's by first character and will group concat the name's as comma separated. You can convert the result from your programming language to array
The output will be like given below
substr(name,1,1) group_conact(name)
A atag1,atag2
C ctag1,ctag2

Query which Find string and increment the count

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..

How to Increment the Table Value With Max ID from Another Table

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