i am working on a search system in which a person is searched by country and their firstname and the last name. For the First and Last name i used concatbut using it with further and condition doesn't seems to work. my query is
select * from users_profile where CONCAT(fname,' ',lname) like '%v %'
or Email like '%v %' AND country='india'
in the above if ise use the query without "and" it works . but i need to specify more conditions and if i simply use and without concat that also works .
select * from users_profile where CONCAT(fname,' ',lname) like '%v %'
or Email like '%v %'
how to make it work with both concat and further and condition i want to apply.
You should not be using CONCAT, let alone any functions around your WHERE columns. Further, you should be specifying a column list, not SELECT *. Also check your order of criteria.
You can try something like this:
SELECT fullname, Email, country
FROM
(SELECT CONCAT(fname, ' ', lname) AS fullname, Email, country FROM users_profile) a
WHERE (a.fullname LIKE '%v %' OR Email LIKE '%v ') AND country = 'india'
AND has a higher precedence than OR, so you need to group your statements more logically.
Something like:
select * from users_profile where (CONCAT(fname,' ',lname) like '%v %'
or Email like '%v %') AND country='india'
should work.
Related
The name column contains items like 'John Smith', 'Elsa John Sanders', 'LilJohn Thomson', 'John'.
How can I structure a query just to return the names with John but not LilJohn.
I cannot do a LIKE '%John%' as it would return 'LilJohn Thomson'.
Looks like this is the same as:
Search for "whole word match" in MySQL
The approach uses a slick regular expression.
Assuming a word is defined by a space, you can do:
where concat(' ', col, ' ') like '% John %'
The following expression should find all "John"s
a = 'John' OR
a LIKE 'John %' OR
a LIKE '% John %' OR
a LIKE '% John'
Please note that some other tricks also work, but may severely hit performance, like
CONCAT(" ",a," ") LIKE "% John %"
Try SIMILAR TO '%+John+%' using the + space wildcard.
You can use REGEXP function.
Try this:
SELECT *
FROM tableA a
WHERE a.name REGEXP '[[:<:]]John[[:>:]]'
How to write a query to retrieve data, only with specific last name first letter.
For example I need a query to retrieve last name starting with A, B, C and E.
Use LIKE and %:
SELECT * FROM people WHERE last_name LIKE 'A%' OR last_name LIKE 'B%' OR last_name LIKE 'C%' OR last_name LIKE 'E%'
Thanks a lot. With the model you have provided me with I have develop the one below:
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name LIKE 'A%' OR vendor_contact_last_name LIKE 'B%'
OR vendor_contact_last_name LIKE 'C%' OR vendor_contact_last_name LIKE 'E%'
ORDER BY vendor_contact_last_name;
Below SQL works for most of the cases
SELECT * FROM people WHERE last_name LIKE '[A-E]%';
The brackets mean anything from alphabets A to E and the percentage (%) means followed by any number of any characters.
If your running SQLite, you can try the below SQL
SELECT * FROM people WHERE last_name >= 'A' and <= 'E';
Hope this helps.
In my SQL, I am using the WHERE and LIKE clauses to perform a search. However, I need to perform the search on a combined value of two columns - first_name and last_name:
WHERE customers.first_name + customers.last_name LIKE '%John Smith%'
This doesn't work, but I wondered how I could do something along these lines?
I have tried to do seperate the search by the two columns, like so:
WHERE customers.first_name LIKE '%John Smith%' OR customers.last_name LIKE '%John Smith%'
But obviously that will not work, because the search query is the combined value of these two columns.
Use the following:
WHERE CONCAT(customers.first_name, ' ', customers.last_name) LIKE '%John Smith%'
Note that in order for this to work as intended, first name and last name should be trimmed, i.e. they should not contain leading or trailing whitespaces. It's better to trim strings in PHP, before inserting to the database. But you can also incorporate trimming into your query like this:
WHERE CONCAT(TRIM(customers.first_name), ' ', TRIM(customers.last_name)) LIKE '%John Smith%'
I would start with something like this:
WHERE customers.first_name LIKE 'John%' AND customers.last_name LIKE 'Smith%'
This would return results like: John Smith, Johnny Smithy, Johnson Smithison, because the percentage sign is only at the end of the LIKE clause. Unlike '%John%' which could return results like: aaaaJohnaaaa, aaaSmithaaa.
try this:
SELECT *
FROM customers
WHERE concat(first_name,' ',last_name) like '%John Smith%';
reference:
MySQL string functions
I'm trying to list all users beginning with a letter, e.g. D
Would the following be the right method of doing this.
Select concat(firstname, '',lastname) from users where concat(lastname) = "D*"
SELECT concat(firstname, '',lastname) FROM users WHERE lastname LIKE "D%"
If you want to use wildcards, you need the LIKE operator. Also, in your where clause you only have one column (lastname), so you don't need concat.
i would try:
select * from users where lastname like 'D%';
For getting list starting with eg: "D":
SELECT firstname FROM users WHERE LEFT(firstname,1)= 'D';
Say I want to search for a user, 'Richard Best'. Is it possible to compare the full name is concatenated first name and last name? I do not have a full name field.
select * from users where last_name + ' ' + first_name like '%richa%'
I am using Mysql
These are equivalent:
select * from users where concat(last_name,' ',first_name) like '%richa%'
select * from users where concat_ws(' ',last_name,first_name) like '%richa%'
This might also work:
select * from users where last_name like '%richa%' or first_name like '%richa%'
Take a look at this thread.
Using mysql concat() in WHERE clause?
select * from users where (first_name + ' ' + last_name) like '%richa%'
In laravel Eloquent you can use whereRaw("concat(first_name,' ',last_name) LIKE %$search%")