MYSQL: select from table where a string in a column matches? - php

I'm trying to select from a table where a column which has a string in it matches some criteria.
the normal way without the string scenario is like this:
SELECT * FROM tablename HWERE columnName='something'
but lets say the columnName contains a value like this:
1,2,3,4 | someemail#yahoo.com
and we want to select from the table where the columnName contains the someemail#yahoo.com. So, how would i need to go about this?
I tried something like this but I'm 100% sure I'm doing it wrong:
SELECT SUBSTRING_INDEX(columnName,' | ',-1) from tableName
because I don't see how the WHERE clause come in that statement!
Could someone please advise on this issue?

You need to use LIKE
SELECT * FROM tablename WHERE columnName LIKE '%someemail#yahoo.com%'

Use LIKE
SELECT * FROM tablename WHERE columnName LIKE '%someemail#yahoo.com%';

Related

SELECT * FROM tbl name WHERE name LIKE 'ask'

Hy, i have a sql tabl name kill, with fields like
ID
name
lname.
There are several names are same, like ali, kiran etc, i want to show all the people with the name ali, so i tried this
SELECT * FROM ask WHERE name LIKE 'ali'
but it shows only the last added ali, please will you tell me the right way to do this. thanks
IF you are trying to find all values containing ali for e.g.
Bali
Alison
etc...
What you need to do is run a wildcard search query, so try this:
SELECT * FROM ask WHERE name LIKE '%ali%'
This will find all values where name contains part of ali in it.
If you want to find all names ending in ali, you can do this:
SELECT * FROM ask WHERE name LIKE '%ali'
If you want to find all names starting with ali, you can do this:
SELECT * FROM ask WHERE name LIKE 'ali%'
etc...
I prefer to use REGEXP, for example:
SELECT * FROM ask WHERE name REGEXP 'ali';
Your query should be as below:-
SELECT * FROM ask WHERE name LIKE '%ali%'
Check this link for detailed info.
SELECT * FROM ask WHERE name LIKE '%ali%';
The Syntax for query in your case is
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
You should use query below to selects all names starting with the letter "ali":
SELECT * FROM ask WHERE name LIKE 'ali%';
You should use query below to selects all names ending with the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali';
You should use query below to selects all names containing the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali%';
Refer This link for tutorials on Like.

How to use OR & AND in WHERE clause in mysql

I'm new to Mysql & have a table where at presently i'm fetching data as
SELECT * FROM tablename WHERE name='$user' AND date='$selecteddate'
Now i want to add 1 more column named status_id where i want to select from to values i.e 1 or 2
I tried this query
SELECT * FROM tablename WHERE (name='Ankit') AND (date='2015-04-23') AND (status_id='1' OR status_id='2')
but didn't work out.
Please help.
Well, you just need the elements concerned by the or clause to be between
parenthesis.
They are not "needed", by the way (you could use precedence order of AND / OR), but I would say this is the best way for readability.
And if status_id data type is int, you don't need the ' ' around 1, and 2.
SELECT *
FROM tablename
WHERE name='Ankit'
AND date='2015-04-23'
AND (status_id=1 OR status_id=2)
By the way, in this case, you may use an IN clause
AND status_id in (1, 2)
Something like this
"SELECT * FROM tablename
WHERE name='Ankit'
AND date='2015-04-23'
AND (status_id='1' OR status_id='2')";
Try with using IN
AND status_id IN (1, 2)

how to implement 'like' and 'in' in mysql

I have search items for eg '%ab', '%avcd%', %sr%', '%pple%' in my PHP variable.
I know LIKE is used in MySQL as:
Select * from tblName where fieldname like '%ab'
I have a long list of search conditions as mentioned earlier and I know that
Select * from tblName where fieldname like in ('%ab','%avcd%','%sr%','%pple%')
doesn't work. What is the best way to resolve the problem?
SELECT * from tblName where fieldname REGEXP 'ab|avcd|sr|pple';
Fulltext search is what you are really looking for.
Try this
Select * from tblName where fieldname like '%ab' or fieldname like '%ab' or fieldname like %sr%' or fieldname like '%pple%'
You can use full text indeexing if the table is myisam.
See http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html for more details.

MYSQL where x in all

I am running a query like this
SELECT ....... WHERE column_name IN (1,2,3)
How can I use the same query and say
SELECT ........WHERE column_name IN (*)
and select all rows?
Edit: The reason I need to do this is because of a bad database design and bad coding in general, for now I need to hack it. If I don't pass the query 1,2,3 .. I need to pass it something so it will return me all records. Is there a way to do it without changing the query? If you don't think this is possible then you should say that it is not possible in your answer.
Just remove the WHERE clause alltogether.
If you absolutely must have the WHERE clause, you could say:
WHERE 1
Or if you need to do the column_name IN bit, you could do something like:
SELECT ....... WHERE column_name IN (1,2,3) OR 1
... IN (SELECT DISTINCT column_name FROM table) ...
If you are trying to filter your return table base on values in another table:
select column_1, column_2
from table_1
where column_1 IN (select column_with_value from tabl_2)

Mysql Select String Function Problem

I want to select records in my table when it matches a row that ends with a particular value.
eg.
if 'oop' is found at the end of a particular record it select the record
Pls how can i go about it
thanks
You can use LIKE:
SELECT *
FROM your_table
WHERE your_column LIKE '%oop'
Note that this query will result in a full scan so it might be slow if you have many rows.
select * from yourtable where somevalue like '%oop'
SELECT *
FROM your_table
WHERE your_column REGEXP 'oop'
Regular Expression Queries can open up some pretty cool extra features that like can't touch.

Categories