I have two tables
USERS Contains user data
Mobilephone
447777744444
447777755555
7777755555
7777766666
MOBILEPHONES Contains mobile phone numbers
Telephone No
7777744444
7777733333
7777755555
7777766666
If I run the follow SQL it returns ONLY numbers that match exactly and does not perform the wildcard search.
SELECT MobilePhones.*, users.FirstName FROM MobilePhones
LEFT JOIN users
ON (users.MobilePhone= MobilePhones.`Telephone No`)
WHERE users.MobilePhone LIKE CONCAT('%', MobilePhones.`Telephone No`, '%')
I get returned
7777755555
7777766666
What I want is
7777755555
7777766666
447777755555
447777744444
I think you probably want to move your WHERE clause into the ON clause of the join, replacing the existing ON clause, which is doing the exact match:
SELECT MobilePhones.*, users.FirstName
FROM MobilePhones
LEFT JOIN users ON users.MobilePhone LIKE CONCAT('%', MobilePhones.`Telephone No`, '%')
Related
My query is:
SELECT *
FROM user u
LEFT JOIN user_detail ud ON u.id = ud.user_id
WHERE CONCAT(ud.first_name,' ',ud.last_name) LIKE 'John Smith%'
I created two index on first_name and last_name column, but I know they didn't work when use CONCAT in where clause because CONCAT scans full table.
Suppose I can't change table to create a new column as full_name. Is there anyway to increase performance in this case?
Noted:
Because the input can be any text such as first name, last name or full name so I use CONCAT and the full query is :
SELECT *
FROM user u
LEFT JOIN user_detail ud ON u.id = ud.user_id
WHERE (CONCAT(ud.first_name,' ',ud.last_name) LIKE 'search%') OR (CONCAT(ud.last_name,' ',ud.first_name) LIKE 'search%')
If your intention is to always use concat, then just create function index rather then indexing to separate columns
CREATE INDEX idx1 ON user_Details (CONCAT(ud.first_name,' ',ud.last_name));
otherwise I suggest you splitting rather than concatenation
WHERE ud.first_name = '?' AND ud.last_name LIKE '?%'
You tried if it is faster with :
ud.first_name = 'John' AND d.last_name like 'Smith%'
The comparison on the first string already may abort and equals is faster than like.
see also: Use '=' or LIKE to compare strings in SQL?
New to php and sql so i will try to explain:
I have a SEARCH field in PHP and i am trying to search by 'ProposalName' that match with what the user enters.
This prints out fine:
SELECT
rec_proposal.ProposalID,
ProposalName,
Status,
researcher.FirstName,
researcher.LastName,
reviewer.FirstName as revFirstName,
reviewer.LastName as revLastName,
reviewer.UserID as revUserID,
review.ReviewDate as revDate,
rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
ON review.UserID=reviewer.UserID
But now using all the columns I need the above code to do something like this
SELECT * FROM rec_proposal WHERE CONCAT (ProposalName) LIKE'%test%'
SO if user enters the word 'test' you would see ProposalName that contains the words test
Just add your WHERE clause, it should work. And as scaisEdge noted in their comment, you don't need CONCAT() if you are just evaluating a single column :
SELECT
rec_proposal.ProposalID,
ProposalName,
Status,
researcher.FirstName,
researcher.LastName,
reviewer.FirstName as revFirstName,
reviewer.LastName as revLastName,
reviewer.UserID as revUserID,
review.ReviewDate as revDate,
rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
ON review.UserID=reviewer.UserID
WHERE rec_proposal.ProposalName LIKE '%test%'
I have a recipe table containing id,recipe_name,recipe_ingredients fields.I am joining these tables in order to search a particular recipe.
My question:"How can i search Veg Noodles, Easy Noodles etc using just "noodles" in like query?
My query:
SELECT r.*,l.*,CASE WHEN (COUNT(l.recipe_id)=0)THEN 0 ELSE 1 END as like_status,
u.name,u.user_image,u.location,u.contact_no,COUNT(l.recipe_id) as total_likes
FROM recipe as r
LEFT JOIN likes as l ON r.recipe_id = l.recipe_id
LEFT JOIN user_detail as u ON r.user_id = u.user_id
WHERE r.recipe_name LIKE '%$search%'
Above query of mine returns only the data containing "noodles".I have tried replacing spaces using replace in above query,but not a luck.So how can I search using LIKE including all whitespaces and spaces?
We can use like these
select * from table_name where table_name like "%Veg Noodles%"
These SQL is useful i think
Thank you.
I cant seem to find a solution for Searching a group_concatenated value,
I have 3 table that are connected with id's
1st table have the same value with 2nd table, but no same value with 3rd,
2nd table have the same value with 1st and 3rd table,
I want to get the value inside 3rd table,
concat the values in accordance to Distinct ID's of 2nd table, display them, and be able to search
this are my tables look like
how do i search for the concatenated values
please if there's a better way, your help is much appreciated?
the query below is what i have so far
$query = $db->prepare("
SELECT
a.problem_encountered,
GROUP_CONCAT(
DISTINCT
c.full_name)
AS
fnames
FROM
maintenance_sheet_table a
LEFT JOIN
mis_incharge_table b
ON
b.mis_incharge_id = a.mis_incharge_id
INNER JOIN
users_table c
ON
c.mis_id=b.mis_id
WHERE
a.problem_encountered
LIKE
:findMe
HAVING
fnames
LIKE
:findMe
GROUP BY a.id ORDER BY a.id
");
$query->bindValue(':findMe', '%' . $keywordSearch. '%');
A potential answer is to filter the Users_table in a subquery. There are a number of different forms of this option, and hard to tell from your data which is required. The one I have below simply returns the users that match the search criteria.
SELECT a.problem_encountered, GROUP_CONCAT(DISTINCT innerc.full_name) AS fnames
FROM maintenance_sheet_table a
LEFT JOIN mis_incharge_table b ON b.mis_incharge_id = a.mis_incharge_id
LEFT JOIN (SELECT c.mis_id, c.full_name
FROM users_table c
WHERE c.full_name LIKE :findMe) innerc ON innerc.mis_id=b.mis_id
WHERE a.problem_encountered LIKE :findMe
GROUP BY a.id
ORDER BY a.id
However, you could also do the concatenation within the subquery if required.
SELECT a.problem_encountered, innerc.fnames
FROM maintenance_sheet_table a
INNER JOIN (SELECT mit.mis_incharge_id, GROUP_CONCAT(DISTINCT ut.full_name) AS fnames
FROM users_table ut
INNER JOIN mis_incharge_table mit ON ut.user_id = mit.user_id
GROUP BY mit.mis_incharge_id
HAVING fnames LIKE :findMe) innerc ON innerc.mis_incharge_id = a.mis_incharge_id
WHERE a.problem_encountered LIKE :findMe
GROUP BY a.id
ORDER BY a.id
Note: I agree with spencer7593, that you shouldn't use the same :findMe variable against 2 separate fields. Even if it works, to a maintenance programmer or even yourself in a few years time, will probably look at this and think that the wrong fields are being interrogated.
You can "search" the return from the GROUP_CONCAT() expression in the HAVING clause. As a more efficient alternative, I suspect you could use an EXISTS predicate with a subquery.
I suspect part of the problem is that your query is referencing the same bind placeholder more than one time. (In previous releases of PDO, this was a restriction, a named bind placeholder could be referenced only once.)
The workaround to this issue is to use a separate bind placeholder, e.g.
HAVING fnames LIKE :findMeToo
And then bind a value to each placeholder:
$query->bindValue(':findMe', '%' . $keywordSearch. '%');
$query->bindValue(':findMeToo', '%' . $keywordSearch. '%');
(With this issue, I don't think PDO issued a warning or error; the effect was as if no value was supplied for the second reference to the named bind placeholder. Not sure if this issue is fixed, either by a code change or a documentation update. The workaround as above, reference a bind placeholder only once within a query.)
Beyond that, it's not clear what problem you are observing.
Your HAVING clause should come after your GROUP BY clause
change
HAVING
fnames
LIKE
:findMe
GROUP BY a.id ORDER BY a.id
to
GROUP BY a.id
HAVING
fnames
LIKE
:findMe
ORDER BY a.id
i have one query that need some changes, and i don't get any clue to do this :
this is my query :
select * from user_data a
left join user_group b
on (a.role like b.role)
actually role value in userdata is (varchar)'staff'
and role value in group is (varchar)'staff;security;finance'
so i don't get result what i expected ..
i imagine the query should be similar to this :
select * from user_data a
left join user_group b
on (b.role like a.role+";%") // using wildcard
and i still don't know the right query using wildcard to this case
any one can help?
You can use CONCAT:
select * from user_data a
left join user_group b
on (b.role like CONCAT(a.role,";%")) // using wildcard
Note - does b.role only have to match a.role at the beginning? what if it was security;staff;finance? You could do CONCAT('%',a.role,'%').
You could do CONCAT('%','a.role','%') to handle matching a.role at any position, but only if you can be sure that you won't have nested roles.
For example: if b.role is staff and a.role is finance;gardenstaff;security, then this row will be returned from the query even though the role is gardenstaff and not staff.
As an alternative, you can use RLIKE instead of LIKE. This is basically a regular-expressions verson of LIKE.
In particular, the regex [[:<:]]staff[[:>:]] will match the whole word staff. The [[:<:]] and [[:>:]] stand for word boundaries, which stop you from matching the staff in gardenstaff.
So, your query could be:
select * from user_data a
left join user_group b
on (b.role RLIKE CONCAT('[[:<:]]',a.role,'[[:>:]]'))
And this would work for b.role being anywhere in the semicolon-separated a.role.