Mysql select column with concat - mysql

i want some correction here. i want to select all people with name fred in database
Here's my query:
SELECT * FROM tdble WHERE CONCAT(name) LIKE CONCAT('%', REPLACE('fred', '')'%')

What you are asking can be simply achieved by either using the "=" operator of the wildcard operator "like" statement.
If you wish to find all records that have an exact match to the name 'Fred' then you should model your query as so:
Select * From tdble Where Name = 'fred'
However, if you want to get all results where the names have 'fred' included in it somewhere use the wildcard operator.
Select * From tdble Where Name like '%fred%'
Also you can further model your query to know where exactly in which form you want 'fred' to appear. Example if you want 'Fred' to be as the last characters of your name string, for instance you wish to get names which ends with fred then model your query like this:
Select * From tdble Where Name like '%fred'
(you will get results like 'alfred', provided there is an alfred in your table)
However if you wish to get all names that begin with fred, model the query like this:
Select * From tdble Where Name like 'fred%'
(you will get results like 'fredinane', provided there is a fredinane in your table)
Cheers

If you want to fetch record with name 'fred', you can simply do Select * from TableName Where Name = 'fred'.
If you want to fetch records which their names' string contain 'fred', you have to use select * from TableName where Name like '%fred%'

Related

how do i write a sql query to select the names that may contain any one of the vowels?

I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'

combining AND operator in mysql

i want to combine conditions by AND operator ..
in my query i want to combine 3 coditions where the result appears when all of them are true.
i want to achive this
SELECT * FROM table WHERE 'condition1' AND ('condition2' AND 'condition3');
i tried this combination but it doesnt work.
this is the actual code
SELECT * FROM planmenu WHERE name LIKE '%$search%' AND type LIKE '%$type%' AND dishcontent LIKE '%$dishcontent%'
where
$search, $type and $dishcontent are php variables. however when i place OR insteade of the second AND it works perfectly. but when i use AND it does not !!
There's nothing wrong with your SQL. Check your data.
create table planmenu (
name varchar(20),
type varchar(20),
dishcontent varchar(20)
);
insert into planmenu values ('a','b','c');
Now if you do this:
SELECT *
FROM planmenu
WHERE name LIKE '%z%'
AND type LIKE '%z%'
AND dishcontent LIKE '%c%';
You'll get zero records back because there are no records matching all three conditions.
If you do this:
SELECT *
FROM planmenu
WHERE name LIKE '%z%'
AND type LIKE '%z%'
OR dishcontent LIKE '%c%';
You'll get a record back because the OR makes it so that the first two conditions OR only the third condition has to be true. It is the equivalent of running this:
SELECT *
FROM planmenu
WHERE (name LIKE '%z%' AND type LIKE '%z%')
OR dishcontent LIKE '%c%';
Check your data. You don't have any records matching all three conditions.
Change the condition to the columns you want to query against, with the values you are trying to get.
SELECT
*
FROM table
WHERE
condition1='something' AND
condition2='somethingelse' AND
condition3='somethingdifferent';

sql select where string matches pattern defined in another table

I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.

Checking if a multi string is found in one of multiple columns in mySQL

i need to find multi string in multi columns in mysql.
select * from myTable WHERE name LIKE '%stack%';
This obviously works. However, what I need to do is that if both name and surname are checked multi string would do something like this:
select * from myTable WHERE (name or surname) LIKE '%stack%','%over%','%flow%';
if similar above code that work does not exist. please tell me how can checking multi string in a column in mySQL with like statement:
select * from myTable WHERE name LIKE '%stack%','%over%','%flow%';
You can keep it simple and do:
select * from myTable WHERE name LIKE '%stack%' OR
name LIKE '%over%' OR
name LIKE '%flow%';
or:
select * from myTable WHERE name LIKE '%stack%' AND
name LIKE '%over%' AND
name LIKE '%flow%';
This doesn't look as nice as the REGEXP from Young, but it doesn't have the unexpected behaviour REGEXP can have given that user input can be anything. With REGEXP you really need to know what you're doing.
You can try:
select * from myTable WHERE name REGEXP 'stack|over|flow';
Though it's not a like solution.

find all the name using mysql query which start with the letter 'a'

I want to find the data from table artists where name is start with letter a, b, c.
i.e.
My o/p should contain
Adam
Alan
Bob
Ben
Chris
Cameron
But not GlAin, doC, dolBie
In MySQL use the '^' to identify you want to check the first char of the string then define the array [] of letters you want to check for.
Try This
SELECT * FROM artists WHERE name REGEXP '^[abc]'
You can use like 'A%' expression, but if you want this query to run fast for large tables I'd recommend you to put number of first button into separate field with tiny int type.
Try this:
select * from artists where name like "A%" or name like "B%" or name like "C%"
One can also use RLIKE as below
SELECT * FROM artists WHERE name RLIKE '^[abc]';
Try this simple select:
select *
from artists
where name like "a%"
I would go for substr() functionality in MySql.
Basically, this function takes account of three parameters i.e.
substr(str,pos,len)
http://www.w3resource.com/mysql/string-functions/mysql-substr-function.php
SELECT * FROM artists
WHERE lower(substr(name,1,1)) in ('a','b','c');
try using CHARLIST as shown below:
select distinct name from artists where name RLIKE '^[abc]';
use distinct only if you want distinct values only.
To read about it Click here.