mysql regexp for all column with same data - mysql

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.

Related

Define a custom ORDER BY order in mySQL using the LIKE word

How can I make a custom order where I sort the rows by NAME where the first rows' name has Bob in it followed by rows with name of Alex in it?
To explain what exactly I mean: I have made the following query to sort result if NAME = 'Bob' and if NAME = 'Alex':
SELECT * FROM table
ORDER BY CASE `NAME`
WHEN 'Bob' THEN 1
WHEN 'Alex' THEN 2
ELSE 3
END
But this only works when the NAME is exactly equal to Bob or Alex. I want to modify it to sort if the NAME has Bob or Alex in it, essentially if NAME LIKE '%Bob%' and NAME LIKE '%Alex%'. I tried something like the following but it does not work.
ORDER BY CASE `NAME`
WHEN LIKE '%Bob%' THEN 1
WHEN LIKE '%Alex%' THEN 2
ELSE 3
END
What is the correct syntax for this?
Use the other form of CASE where you specify a condition in WHEN rather than a value.
ORDER BY CASE
WHEN NAME LIKE '%Bob%' THEN 1
WHEN NAME LIKE '%Alex%' THEN 2
ELSE 3
END

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 Select when case

i have those tables and i want to check the same attribute at the same time:
Person
id---name
1----Paul
2----Tom
3----Jim
Age
id---wert------personId
1----28--------1
2----25--------1
3----30--------3
i want to do something like this.
select * from Person p, Age where personId = p.id and CASE WHEN
name = 'Paul' THEN Age > 28 WHEN name = 'Tom' THEN Age <....
How it is possible? With a CASE THEN in the WHERE clause? Please don't think about the structure of the table but only about the principle.
Any Ideas?
Thank
What you're trying to do is possible like so
select
*
from
Person p, Age
where personId = p.id
and CASE
WHEN name = 'Paul' THEN Age > 28
WHEN name = 'Tom' THEN Age <....
WHEN expr then expr that evals to bool
END
CASE WHEN THEN I believe need to be part of an expression, not an expression themselves. It's common use case is in the select, however you can use it in the where.
In a select this would be:
select
case name
when 'paul' then 28
when 'jim' then 30
end
from X
To use it within an where you would do this:
select * from X where age = case name when 'jim' then 28 else 30 end
Depending on what you are attempting to achieve, you may want to consider OR statements instead of case in your where clause.

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