SQL find part of string - php

I met following problem:
I've got table 'students' with columns: 'id_student', 'name' and 'surname'.
Then I got a variable that contains full_name (example: 'James Bond')
I need to make a query that selects 'id_student' where 'name' and 'surname' matches whole name.
I tried to do this in several ways, like:
SELECT id_student FROM students WHERE name+' '+surname LIKE full_name
but it doesn't work. Any idea?

Your code should work in a database where + is used for string concatenation. The LIKE is fine, but = seems sufficient:
SELECT id_student
FROM students
WHERE CONCAT(name, ' ', surname) = full_name;
Note that unexpected characters and small differences in spelling would cause the comparisons to fail.

MySQL does not support string concatenation using plus, at least not by default. Instead, we can use the CONCAT function. The LIKE expression you want here is:
full_name LIKE 'James%Bond'
Here we want any record which starts with James and ends with Bond.
SELECT id_student
FROM students
WHERE full_name LIKE CONCAT(name, '%', surname);

Maybe you should try something like:
SELECT id_student
FROM students
WHERE CONCAT_WS(" ", name, surname)
LIKE full_name
CONCAT_WS or CONCAT is the MySQL function to join strings. The former allows you to define a separator, a space in your case.

Related

SQL WHERE CONCAT LIKE NOT

I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.

search without hyphen in mysql

Mysql database contains below kind of values :
'AE01-1056 Ricoh Aficio' OR 'Ricoh AE01-1087 (AE01-1069)' etc
AS if am a normal user i will search the product name in simple text like
AE011056 ... but the result is not found.
i hav tried this query:
$q="SELECT * FROM mytable WHERE (p.product_name LIKE '$name%' OR c.category_name LIKE '$name%' OR pm.name LIKE '$name%')";
What change should i make in my query to get the product , because i have tried LIKE operator & it's not working for me.
Use replace function
$q="SELECT * FROM mytable
WHERE (REPLACE(p.product_name,'-','') LIKE '%$name%'
OR REPLACE(c.category_name,'-','') LIKE '%$name%'
OR REPLACE(pm.name ,'-','') LIKE '%$name%')";
I think there are only two ways:
1. Manipulate search string
If you knwo, users are often search for a code and don't use nessesary hyphens, check the searchstring bevor searching if it follows a given format an insert the hypen if it is missing.
2. replace all hyphens in the where-statement
see http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
Depending on your setup, solution one might be the more performant solution, since you only have to do one instead multiple stringmanipulations.

Remove quote from query inside : IN() MySql

If i give hard coded value inside query it works, but not in case of sub query or column given.
Here is small example of issue i am facing :
Following both query is a type of sub query, like its part of another query, so don't think that where is table 'm' and something else, as it is working already.
So, my query like :
1)
SELECT GROUP_CONCAT(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u
FROM users a_u
WHERE a_u.id IN(m.associated_admin)
GROUP
BY m.id
And m.associated_admin will return a quoted string like '1,10' so this will not work because of its a string.
2)
SELECT GROUP_CONCAT(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u
FROM users a_u
WHERE a_u.id IN(1,10)
GROUP
BY m.id
If i write hard code like 1,10 it works, because it is not a string
So first one is not works because that query is part of another query as a sub query.
And i am sure this question couldn't be duplicate as i am facing it like in this way so any help would be appreciate, thanks reader!
Based on your comments, you need something like:
SELECT GROUP_CONCAT(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u
FROM users a_u
WHERE FIND_IN_SET(a_u.id, TRIM(BOTH '\'' FROM m.associated_admin))
GROUP
BY m.id
This will first trim the quotes from m.associated_admin and then use FIND_IN_SET instead of IN so that you can use a string with comma-separated values.
You can just create a subquery in IN for example:
SELECT group_concat(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u
FROM users a_u WHERE a_u.id IN(
SELECT id FROM mytable WHERE id IN(1,10)
) GROUP BY m.id

PHP SELECT distinct doesn't echo variable?

$query = "SELECT distinct('case'), firstname, lastname
FROM cases ";
Why can't "case" be echoed? It is a number..
It will echo when the query is like this:
$query = "SELECT case, firstname, lastname
FROM cases ";
First, case should not be in quotes, it should be in backticks. Second, DISTINCT always applies to the whole row regardless of any parentheses you may have around any of your expressions. In your query your parentheses are misleading and should be removed.
SELECT DISTINCT `case`, firstname, lastname FROM cases
If you want to return a single aggregated row for each case you need to use "GROUP BY `case`", but then you need to consider what should be returned for the other values.
The case is not echoed because you might be trying to echo $row['case'] which wont work because the feild that is returning is not case but distinct(case)
And you need to avoid the single quote and either give no quotes or use a backquote; else mysql will take case as a literal string
so change the query to
SELECT
DISTINCT(case),
firstname,
lastname
FROM
cases
And if you want to access the case as $row['case'] the use as like
SELECT
DISTINCT(case) as case,
firstname,
lastname
FROM
cases

How can I avoid separating the search query to search a MySQL database?

When a user tries to search for people on our system, we've noticed that they often type a full name for their search.
How can we enable full name searching on our database when first name and last name are stored in different columns?
A working but neither fast nor reliable solution would be:
SELECT foo
FROM bar
WHERE CONCAT(firstname, ' ', lastname) = $search_name
(Not sure about MySQL CONCAT syntax atm, you might need to concat twice)
There is a high chance that this won't use any indexes, slowing down the search a lot.
A better solution would be to just split the name:
$names = explode(' ', $search_name);
SELECT foo
FROM bar
WHERE firstname = $names[0]
AND lastname = $names[1]
Even better:
Provide two input boxes and label them first and lastname so users will enter the search correctly.
Even better - If you are using CakePHP 1.3 then simply create a 'full_name' virtual field. Do your query against that field.
http://book.cakephp.org/view/1609/Creating-virtual-fields
http://book.cakephp.org/view/1610/Using-virtual-fields
class User extends AppModel {
...
var $virtualFields = array(
'full_name' => 'CONCAT( User.first_name, " ", User.last_name )'
);
...
}
Since Cake treats virtual fields as regular fields for the primary model you can simply do a find like so.
$User->find( 'all', array(
'conditions' => array(
'User.full_name' => $search_name
...
),
...
);
You could even throw in some MySQL LIKE goodness in the conditions
$User->find( 'all', array(
'conditions' => array(
'User.full_name LIKE' => '%' . $search_name . '%',
...
),
...
);
This syntax means the end-user can put part of the first, part of the last or the whole name into the search field and still get results without worrying about if the code is doing finds on first, last or both.
Your 99% case is probably going to be this:
Split the string on the first whitespace
Assume the first part is the first name, and the 2nd part is the last name
Depending on the number of users in your system, and the... "creativeness" of their names, you may get a few false negatives from this approach.
You could also consider providing separate fields to the user.
You could set up a single fulltext index on first_name and last_name. Then, just include a test for MATCH (first_name, last_name) AGAINST ($query). Or if you have more than just that field to search against, add those to the index as well.
Let SQL do the work for you.
How do you search the people in your DB?
If you have a SQL query you can do it somehow like that:
Separate the search text into parameters. so you have firstname and lastname in 2 separate parameters. (Stackoverflow: How to split a string in php)
then you can use the different parameters to build a query that fits your needs:
select *
from People
where
(firstname LIKE param1 AND lastname LIKE param2) OR
(firstname LIKE param2 AND lastname LIKE param1) OR ...

Categories