Imagine a table like:
string
12345
1234567
13254
123
I just want to select those rows that contain: 1,2,3,4,5 in any order. And also those that even being shorter, all the letters in it at least are from that "array".
Selected strings might have ONLY those chars 1,2,3,4,5 in any order, and not of them necesary.
Selected:
12345
123
1324
Not Selected:
123a
1123
Off the top of my head.....
SELECT *
FROM yourtable
WHERE yourcolumn RLIKE '/^([1-5])+$/';
Try this
SELECT *
FROM table_name
WHERE column_name RLIKE '^([1-5])+$';
Related
I am basic on SQL queries and I need some help.
I have to select all string values which contains number e.g. 7 only on specific position in that string.
For example:
I have string: 987654321 and if on position 3 I will have number 7, then it should be selected.
So in example this string will be selected, because on 3rd position I have number 7.
Is there any SQL function for that, or something which could help me?
EDIT:
Example table
TABLE
Numbers Value
987654321 1
123456789 2
789009871 3
654321092 4
847949372 5
Output:
TABLE
Numbers Value
987654321 1
847949372 5
Statement:
SELECT table.numbers
FROM TABLE
WHERE substr(table.numbers,3,1)='7' <--- what to do here? --->
Many thanks in advance.
For a regex option, you may use MySQL's REGEXP operator:
SELECT *
FROM yourTable
WHERE num REGEXP '^[0-9]{2}7';
On Oracle, you could use REGEXP_LIKE:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(num, '^[0-9]{2}7');
You should use case statement.
select case when substr(stringcol, 3,1) = '7' then stringcol else "not valid" end as stringcol from <Table Name>
I have a table which has column name and having stored
|name|
Lebron James C. Durant
And I want to select and filter incomplete name. But the result is null.
SELECT * from partners where name LIKE "%Lebron James Durant%"
Here's my expected result in this query.
|name|
Lebron James C. Durant
Replace spaces with % signs.
SELECT * from partners where name LIKE '%Lebron%James%Durant%';
If you want to query an arbitrary string, use like this (#n may be your query parameter):
SET #n = 'Lebron James Durant';
SELECT * from partners where name LIKE CONCAT('%', REPLACE(#n, ' ', '%'), '%')
I have a table with json values like this:
-Table 1
id | name | data
------+----------+---------------
1 | Test | {"city_id": 3, "email":"test#test.com", "city_name":"something"}
2 | Test 2 | {"city_id": 1, "email":"test2#test2.com", "city_name":"another"}
3 | Test 3 | {"city_id": 6, "email":"test3#test3.com", "city_name":"blahblah"}
Now I want SELECT records with order by data.city_name, so I use this code:
SELECT id, name, JSON_EXTRACT(data, 'city_name') AS cityName
FROM table1
ORDER BY cityName ASC
but this query cannot sort my records correctly !
P.S: city_name have UTF-8 characters.
you do not seem to be using JSON_EXTRACT() properly, try with:
SELECT id, name, JSON_EXTRACT(data, '$.city_name') AS cityName
FROM demo ORDER BY cityName ASC
Demo Fiddle
I usually cast the JSON value (->>) to the correct type in order to sort properly:
SELECT id, name, data->>'$.city_name' AS cityName
FROM table1
ORDER BY CAST(cityName AS CHAR) ASC
Otherwise, you end up sorting as a blob (binary), which are treated as binary strings (byte strings) and thus they have the binary character set and collation, and comparison and sorting are based on the numeric values of the bytes in column values (ref).
the easiest way in my opinion
SELECT * FROM YourTable order by data->"$.city_name" desc
Check This.
SELECT Id ,name,SUBSTRING_INDEX(SUBSTRING_INDEX(data,'city_name":"',-1),'"',1) as CITY
FROM tempjson
order by SUBSTRING_INDEX(SUBSTRING_INDEX(data,'city_name":"',-1),'"',1)
OutPut :
i have a table like this.
id name father_name age
1 raja first 12
2 second first 13
When i execute a below query.
SELECT * FROM class WHERE name REGEXP 'first|12'
OR father_name REGEXP 'first|12'
OR age REGEXP 'first|12'
I getting below as a results.
id name father_name age
1 raja first 12
2 second first 13
But I want below as a result.
id name father_name age
1 raja first 12
If I change name with or condition. I can achieve.But
As same time the user given raja|12 means
SELECT * FROM class WHERE name REGEXP 'raja|12'
OR father_name REGEXP 'raja|12'
OR age REGEXP 'raja|12'
I want the result like this.
id name father_name age
1 raja first 12
Because i dont know which one will get from user name or father_name or age or all the three. So if i get all the three there is no problem. But when i get a singl or doble values so i need to search regarding that.
Is there any possibility to get those results?
You seem to want and instead of or, but this is complicated by the fact that you don't seem to care about name. I'm tempted to say:
SELECT *
FROM class
WHERE father_name REGEXP 'first|12' AND
age REGEXP 'first|12';
I'm not sure what name is doing in the WHERE clause.
EDIT:
It occurs to me that you want the best matching row. If so:
SELECT *
FROM class
WHERE name REGEXP 'first|12' OR
father_name REGEXP 'first|12' OR
age REGEXP 'first|12'
ORDER BY ((name REGEXP 'first|12') + (father_name REGEXP 'first|12') + (age REGEXP 'first|12')) DESC
LIMIT 1;
Please be aware that REGEXP 'xx|yy' means this matches xx OR yy so your result is correct for that query.
To get the result you want, you will have to clarify what you want to achieve. I assume you want the follwing: select all rows where the father is first AND age is 12
You can achieve this by using:
SELECT * FROM mytable WHERE father_name like 'first' AND age = 12;
You can try this solution here: Relevant SqlFiddle.
Edit1: Possible alternative soultion after more comments by OP:
SELECT * FROM mytable WHERE
father_name IN ('first', '12') AND age IN ('first', '12')
OR
father_name IN ('first', '12') AND name IN ('first', '12')
OR
name IN ('first', '12') AND age IN ('first', '12');
You can try this solution here: Relevant SqlFiddle.
assume that we are performing search using keywords: keyword1, keyword2, keyword3
there are records in database with column "name":
1: John Doe
2: Samuel Doe
3: John Smith
4: Anna Smith
now Query:
SELECT * FROM users WHERE (name LIKE "%John%" OR name LIKE "%Doe%")
it will select records: 1,2,3 (in this order)
but i want to order it by keyword
in example keyword1=John, keyword2=Doe
so it should be listed by keywords: 1,3,2 (because i want to perform search for "Doe" after searching for "John")
I was thinking about SELECT DISTINCT FROM (...... UNION .....)
but it will be much easier to order it somehow in another way (real query is really long)
are there any tricks to create such order?
order by case
when name LIKE "%John%" then 1
when name LIKE "%Doe%" then 2
else 3
end
To build on RedFilter's answer, you could make the rows that have both keywords to be at the top:
order by case
when (name LIKE "%John%" and name LIKE "%Doe%") then 1
when name LIKE "%John%" then 2
when name LIKE "%Doe%" then 3
end
Read up on Boolean Fulltext Searches, with which you can do ordering.
SELECT *
from
(
SELECT u.*, 1 OrderNum
FROM users
WHERE (name LIKE "%John%")
UNION
SELECT u.*, 2 OrderNum
FROM users
WHERE (name LIKE "%Doe%")
)
Order by OrderNum
My example will Order all of the John's Alphabetically followed by the Doe's.
ORDER BY CASE
WHEN name LIKE "John%Doe" THEN CONCAT('a',name)
WHEN name LIKE "John%" THEN CONCAT('b',name)
WHEN name LIKE "%Doe" THEN CONCAT('c',name)
ELSE name
END