How can I select to like wildcards in mysql - mysql

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, ' ', '%'), '%')

Related

SQL condition where 2 values share common character

I'm trying to create a query that lists staffID, staffName and staffDOB, but only of staff that first and last names begin with the same letter. So I have both staffFirst and staffLast as individual columns, will join them together. I will not be customising staffID and staffDOB. I would like it to return the name of staff like adam apple = a apple, so the output would look like:
staffID | staffName | staffDOB
------------------------------
1 | A Apple | 12/10/99
.... | .... | ....
All columns are in the same table "N_Staff". I am using HeidiSQL which I believe uses MySQL. I know how to grab the data of each column, though it is selecting the first letters of both first and last names and comparing them which is confusing me as it is not an specific letter I am looking for but any letter that is common on both tables of index [0].
Thus far:
SELECT staffID FROM N_Staff,
SELECT staffFirst, staffLast AS staffName
FROM N_Staff WHERE ... , --perhaps should be using LEFT ?
SELECT staffDOB from N_Staff;
How about:
SELECT staffID, CONCAT(LEFT(staffFirst,1), ' ', staffLast) AS staffName, staffDOB
FROM N_Staff
WHERE LEFT(staffFirst,1) = LEFT(staffLast,1)
use this:
SELECT * FROM NS_WORDS;
mani
nikhil
sugandh
mining
_lkdnsad
_lkdndsadnjas
_lk
_ja
_ls
_lsa
nikhil nikhil
nikhil name
SELECT * FROM NS_WORDS
where not( to_char(SUBSTR(a,1,1))=to_char(substr(a,instr(a,' ',1,1),1)));
output:
nikhil nikhil
nikhil name
your where will go like:
where not( to_char(SUBSTR(staffName ,1,1))=to_char(substr(staffName
,instr(staffName ,' ',1,1),1)));

SQL Query Select Clause, need a solution to not return few rows

I have a column in a static table like this:
Vehicles
-------------
Bike
Truck
car_2018
car_2019
car_2020
car_2021
Bus
The select query needs to fetch only the car row based on the year of query (for example now its 2018, if I run this next year, it should get back _2019) long with the rest of the rows that's not based on years. Need a solution for this.
So far I have this:
SELECT Vehicles
FROM VehicleMaster
WHERE 'some where clause based on other columns'
select Vehicles
from table_name
where Vehicles like '%2018'
union all
select Vehicles
from table_name
where Vehicles not like '%car%'
You can use substring_index to split that field by underscore _ and query based on that:
CREATE TABLE vehicles(f1 varchar(30));
INSERT INTO vehicles VALUES ('Bike'),
('Truck'),
('car_2018'),
('car_2019'),
('car_2020'),
('car_2021'),
('Bus');
SELECT f1
FROM vehicles
WHERE
f1 NOT LIKE 'car%'
OR (f1 LIKE 'car%' AND substring_index(f1, "_", -1) = YEAR(CURDATE()));
+----------+
| f1 |
+----------+
| Bike |
| Truck |
| car_2018 |
| Bus |
+----------+
SqlFiddle here
You can use regex to exclude all car_#### rows, except for the current year. Assuming that your Vehicles column is called name, this should work for you:
select *
from Vehicles
where
(
-- Exclude all car_####
not trim(name) REGEXP '^car_[0-9]{4}$'
-- Except for the current year
or name = concat('car_', year(now()))
)
I think you want:
select t.*
from t
where t.vehicle = concat('car_', year(curdate())) or
t.vehicle not regexp '[0-9]{4}$'
If you want a general purpose "any current year or any without a year", then:
select t.*
from t
where t.vehicle like concat('%_', year(curdate())) or
t.vehicle not regexp '[0-9]{4}$'

mysql regexp for all column with same data

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.

mySQL Select ONLY data that contains some letters in any order

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])+$';

Mysql: Order by like?

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