I got a table with the following values
titles:
Accountant
Auditor
Bookkeper
Forensic Accountant
Tax Accountant
I'd like to select all the titles that don't contain "Accountant" in it, i'm currently trying with
SELECT * FROM careers WHERE title NOT REGEXP '^Accountant'
But it still selects "Forensic Accountant" and "Tax Accountant"
Any thoughts?
Thanks in advance!
use not like instead of regexp:
SELECT * FROM careers WHERE title NOT LIKE '%Accountant%'
See the SQLFiddle
You can use LIKE instead of REGEXP like this:
SELECT * FROM careers WHERE title NOT LIKE '%Accountant%';
The regex is wrong, you are using the caret(^) , which means the words you are excluding should start with Accountant, instead remove the caret and you should be good
SELECT * FROM careers WHERE title NOT REGEXP 'Accountant'
To exclude something user defined, based on what jobtitle is selected, do something like the following
SELECT * FROM careers WHERE title NOT REGEXP (SELECT regexMatch FROM careers WHERE title = "Tax Accountant")
Related
I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
If it is just for searching then you may be able to use CONCATENATE_WS.
This would allow wild card searching.
There may be performance issues depending on the size of the table.
SELECT *
FROM pages
WHERE CONCAT_WS('', column1, column2, column3) LIKE '%keyword%'
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
If either clause matches then the record will be returned.
For more about what you can do with MySQL SELECT queries, try the documentation.
If your table is MyISAM:
SELECT *
FROM pages
WHERE MATCH(title, content) AGAINST ('keyword' IN BOOLEAN MODE)
This will be much faster if you create a FULLTEXT index on your columns:
CREATE FULLTEXT INDEX fx_pages_title_content ON pages (title, content)
, but will work even without the index.
1)
select *
from employee em
where CONCAT(em.firstname, ' ', em.lastname) like '%parth pa%';
2)
select *
from employee em
where CONCAT_ws('-', em.firstname, em.lastname) like '%parth-pa%';
First is usefull when we have data like : 'firstname lastname'.
e.g
parth patel
parth p
patel parth
Second is usefull when we have data like : 'firstname-lastname'. In it you can also use special characters.
e.g
parth-patel
parth_p
patel#parth
Here is a query which you can use to search for anything in from your database as a search result ,
SELECT * FROM tbl_customer
WHERE CustomerName LIKE '%".$search."%'
OR Address LIKE '%".$search."%'
OR City LIKE '%".$search."%'
OR PostalCode LIKE '%".$search."%'
OR Country LIKE '%".$search."%'
Using this code will help you search in for multiple columns easily
SELECT * FROM persons WHERE (`LastName` LIKE 'r%') OR (`FirstName` LIKE 'a%');
Please try with above query.
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 can I perform this type of search?
Here is my table:
Name Category
---------------------------------
Company1 food,services
Company2 garden,gifts,services
Company3 other,auto
and here is the query:
$search = $_GET['search'];
"SELECT * FROM companies WHERE category IN ('$search')";
The above search variable looks like mydomain.com?search=garden,services
The idea is to get all relevant rows if any of the matching keywords are present within Category field.
The simplest SQL search is probably a LIKE search:
SELECT * from companies where category LIKE ("%$search%");
You can also do a REGEXP search:
SELECT * from companies where category REGEXP ("$search");
What many have ended up doing, though, is fulltext searching. You can configure MySQL to do this, or perhaps you'll want to try a solution like SphinxSearch.
I was writing a products search engine for a pet project website and ran into a problem. How do I return products ordered by "how much of a match" they were for the query? Right now I have this, which will only return products where the query matches the beginning.
$sql = "SELECT * FROM `Products` WHERE name LIKE '$query%'";
Placing a % in front of $query like so
$sql = "SELECT * FROM `Products` WHERE name LIKE '%$query%'"
achieves the desired output but it is not ordered correctly. For example, the user searches for "p" and they get a list like: [A-word with p in it], [B-word with p in it], [P-word]. I would like the P-word to show up first, and the A & B words to show up after it. Is this possible with only MySQL? I've looked into ORDER BY but that orders by columns AFAIK.
You can do this with order by:
SELECT *
FROM `Products`
WHERE name LIKE '%$query%'"
ORDER BY (name like '$query%') desc,
name;
The first clause will put the names that start with $query first.
My site is like imdb.com - it stores movie details and actor's names. In my actor field, I'm storing actors name like "name1,name2,name3,name4".
My site has the option to search movies by actor name. My code for this is below:
SELECT * FROM imdb WHERE actors LIKE '%%actorname%%'
The problem is that if a visitor searches for 'Lal', then movies with actor's named 'Mohanlal' and 'Lal' will both appear. I only want to list the 'Lal' movies. How can I write the query to do that?
Try the following query:
SELECT * FROM `imdb` WHERE `actors` RLIKE '[[:<:]]Lal[[:>:]]'
Even better, and easier:
SELECT * FROM `imdb` WHERE find_in_set('Lal',actors)
Make the query like this:
SELECT * FROM imdb WHERE actor LIKE 'actorname'
and put a text next to your inputfield to let them use wildcards for search. Then, if they want to search for 'Lal', the will only find 'Lal', if they want to search for everything that ends on 'lal' (%lal), they will find Mohanlal and Lal.
You should have created separated table for actors, anyway we will try to find solution of what we are left to.
SELECT * FROM imdb WHERE actors LIKE 'Lal,%' OR actors LIKE '%,Lal%' OR actors = 'Lal'
SELECT * FROM imdb WHERE actor LIKE '%,lal' or actor LIKE 'lal'
I think this is what you need:
SELECT whatever
FROM whereever AS t
WHERE t.actors = SOUNDEX('test')