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).
Related
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
For experimental purposes only.
I would like to build a query but not querying data extracted for any table but querying data provided in the query it self. Like:
select numbers.* from (1, 2, 3) as numbers;
or
select numbers.* from (field1 = 1, field2 = 2, field3 = 3) as numbers;
so I can do things like
select
numbers.*
from (field1 = 1, field2 = 2, field3 = 3) as numbers
where numbers.field1 > 1;
If the solution is specific for a database engine could be interesting too.
If you wanted the values to be on separate rows instead of three fields of the same row, the method is the same, just one row per value linked with a union all.
select *
from(
select 1 as FieldName union all
select 2 union all
select 3 union all
select 4 union all -- we could continue this for a long time
select 5 -- the end
) as x;
select numbers.*
from(
select 1 ,2, 3
union select 3, 4, 5
union select 6, 7, 8
union select 9, 10, 11 -- we could continue this for a long time
union select 12, 13, 14 -- the end
) as numbers;
This works with MySQL and Postgres (and most others as well).
[Edit] Use union all rather than just union as you do not need to remove duplicates from a list of constants. Give the field(s) in the first select a meaningful name. Otherwise, you can't specify a specific field later on: where x.FieldName = 3.
If you don't provide meaningful names for the fields (as in the second example), the system (at least MySQL where this was tested) will assign the name "1" for the first field, "2" as the second and so on. So, if you want to specify one of the fields, you have to write expressions like this:
where numbers.1 = 3
Use the values row constructor:
select *
from (values (1),(2),(3)) as numbers(nr);
or using a CTE.
with numbers (nr) as (
values (1),(2),(3)
)
select *
from numbers
where nr > 2;
Edit: I just noticed that you also taggeg your question with mysql: the above will not work with MySQL, only with Postgres (and a few other DBMS)
You can use a subquery without table like so:
SELECT
numbers.*
FROM (
SELECT
1 AS a,
2 AS b,
3 AS c
UNION
SELECT
4,
5,
6
) AS numbers
WHERE
numbers.a > 1
If you like queries to always have a table referenced there is a Psuedo table that always has 1 row and no columns called DUAL, you can use it like so:
SELECT
numbers.*
FROM (
SELECT
1 AS a,
2 AS b,
3 AS c
FROM
DUAL
UNION
SELECT
4,
5,
6
FROM
DUAL
) AS numbers
WHERE
numbers.a > 1
I know that this question has been asked in various forms but my requirement happens to be a bit different.
Suppose I have a table that contains data as follows:
ID NAME VALUE
-----------------------------
1 ABC-2-2 X
2 PQRS-1-3 Y
3 ABC-3-2 Z
4 PQRS-1-4 A
5 PQRS-3-4 B
6 MNO-2-1 C
7 AAA-1 D
8 BBB-2 E
9 CCC-3 F
Now, the output that I'm expecting should look something like this:
ID NAME VALUE
-----------------------------
7 AAA-1 D
2 PQRS-1-3 Y
4 PQRS-1-4 A
8 BBB-2 E
6 MNO-2-1 C
1 ABC-2-2 X
9 CCC-3 F
3 ABC-3-2 Z
5 PQRS-3-4 B
Note that this is not a direct alpha-numeric sort. Instead, the value before the first "-" is ignored and the fields are sorted on what is after the first "-" in the name.
I'm not very familiar with PL/SQL and any kind of help on this would be appreciated.
Thanks.
PS: Note that this should work on both Oracle and MySQL.
For your example this would suffice (Oracle syntax):
ORDER BY SUBSTR(name,4)
If the number of characters before the first hyphen can vary, you can do this (again Oracle syntax):
ORDER BY SUBSTR(name,INSTR(name,'-')+1)
However that won't work if you have codes like:
AAA-10-1
AAA-8-1
AAA-9-1
and expect AAA-10-1 to appear after AAA-9-1. Then you will need to parse it further:
ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name,'-',1,2)-INSTR(name,'-')-1),10,'0'),
LPAD(SUBSTR(name,INSTR(name,'-',1,2)+1),10,'0')
(NB I have used LPAD(x,10,'0') to turn a value like '1' into '0000000001' and so on, rather than use TO_NUMBER since this could fail if there are any non-numerics in your data.)
Example:
with data as
(
select 'AAA-1' name from dual
union all
select 'PQR-1-4' name from dual
union all
select 'PQR-1-3' name from dual
union all
select 'AAA-10-10' name from dual
union all
select 'AAA-10-1' name from dual
union all
select 'AAA-9-10' name from dual
union all
select 'AAA-9-1' name from dual
)
select *
from data
ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name,'-',1,2)-INSTR(name,'-')-1),10,'0'),
LPAD(SUBSTR(name,INSTR(name,'-',1,2)+1),10,'0');
Output:
NAME
---------
PQR-1-3
PQR-1-4
AAA-9-1
AAA-9-10
AAA-10-1
AAA-10-10
AAA-1
And if AAA-1 should come first:
ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name||'-','-',1,2)-INSTR(name,'-')-1),10,'0'),
LPAD(SUBSTR(name,INSTR(name||'-','-',1,2)+1),10,'0') nulls first
Not sure about mysql syntax, but you can do this in oracle:
select * from <your_table>
order by substr(name, 5)
in mssql the syntax of finding your problem is :
select * from mytable order by substring(name,PATINDEX('%-%',name)+1,len(name)-PATINDEX('%-%',name))
SqlFiddle
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_%')
For example I have a table MyTable like this:
Id Value Rate
1 10
2 30
3 40
4 60
5 100
Rate is defined as: Rate = [ Value(id) - Value(5) ] / [ 5 - id ] , for id from 1 to 4.
I'm thinking of doing this:
INSERT INTO MyTable (Id, Rate)
SELECT Id,
??? real work goes here
FROM MyTable
LIMIT 4
ON DUPLICATE KEY UPDATE Rate=VALUES(Rate);
But can someone help me how to do the "rate" part? Thanks!
Another thing, if I define Id like this:
Id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
Is it possible the program could assign the value of Id not continuous, for example, the values of Id like this: 1, 2, 4, 5, 6? I'm asking because if this is possible, my program would fail because I assume the Id values are continuous.
In addition, is it possible the value of Id not starting from 1? For example, the values of Id like this: 2, 3, 4, 5, 6? I'm asking because if this is possible, my program would fail because I assume the Id value always starts from 1 and if there are five rows the last one would be 5.
Thanks for helping!
You can write:
UPDATE MyTable
SET Rate =
( Value - (SELECT Value From (SELECT Value FROM MyTable WHERE id = 5) AS t) )
/ (5 - id)
WHERE id BETWEEN 1 AND 4
;