write select query for the below requirement - mysql

This is my users table:
http://ezinfotec.com/Capture.PNG
I need to select all rows those are not contain 2 in except column. How to write a query for this using php & Mysql.
The result i expect for this query is only return last row only.
Thank you.

Don't store comma separated values in your table, it's very bad practice, nevertheless you can use FIND_IN_SET
SELECT
*
FROM
users
WHERE
NOT FIND_IN_SET('2', except)

Try this:
SELECT *
FROM users
WHERE CONCAT(',', except, ',') NOT LIKE '%,2,%'

this should work for you
SELECT *
FROM table
WHERE table.except NOT LIKE '%,2%'
OR table.except NOT LIKE '%2,%';

Related

Query to select data from mysql with specific condition

In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.

Query MySQL field for LIKE string

I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'

How to query MySQL if data in one field look like 32;13;3;33 and the condition is where 3

I have a table in which one column is filled with data like 32;3;13;33;43
so
SELECT * FROM table;
gives something like
name ids
vegetables 13;3;63
fruits 37;73;333
When I'm querying MySQL like
SELECT * FROM table WHERE ids LIKE '%3%'
it gives me both records but obviously I want only this containing 3.
How to query MySQL correctly?
Try to use:
SELECT * FROM table WHERE CONCAT(';',ids,';') LIKE '%;3;%'
You will need to cover the case where it's the first in the list and the last.
SELECT * FROM table WHERE ids LIKE '%;3;%' OR LIKE '%;3' OR LIKE '3;%'
You can use FIND_IN_SET, if you replace the ; with a , before checking the value:0
SELECT *
FROM table
WHERE FIND_IN_SET('3', REPLACE(ids, ';', ','))

"Merge" IN and LIKE operator in Mysql

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.

MySQL Like multiple values

I have this MySQL query.
I have database fields with this contents
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
Why does this like query does not work?
I need the fields with either sports or pub or both?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
Faster way of doing this:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
is this:
WHERE interests REGEXP 'sports|pub'
Found this solution here: http://forums.mysql.com/read.php?10,392332,392950#msg-392950
More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
The (a,b,c) list only works with in. For like, you have to use or:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
Why not you try REGEXP. Try it like this:
SELECT * FROM table WHERE interests REGEXP 'sports|pub'
You can also use REGEXP's synonym RLIKE as well.
For example:
SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
Don't forget to use parenthesis if you use this function after an AND parameter
Like this:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
Or if you need to match only the beginning of words:
WHERE interests LIKE 'sports%' OR interests LIKE 'pub%'
you can use the regexp caret matches:
WHERE interests REGEXP '^sports|^pub'
https://www.regular-expressions.info/anchors.html
Your query should be SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
What I understand is that you store the interests in one field of your table, which is a misconception. You should definitively have an "interest" table.
Like #Alexis Dufrenoy proposed, the query could be:
SELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0
More information in the manual.
More work examples:
SELECT COUNT(email) as count FROM table1 t1
JOIN (
SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
) t2
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference';
Task was count participants at an event(s) with filter if email extension equal to multiple company domains.