I have an array like array('first','12'). So i need to select the table using like condition. which fields have those values.
Example Query:
SELECT * FROM `class` WHERE name LIKE '%first%'
OR father_name LIKE '%first%'
OR age LIKE '%first%'
OR name LIKE '%12%'
OR father_name LIKE '%12%'
OR age LIKE '%12%'
I need to find all the fields with or condition. Is this good or have any other options to find an array of values in all fields.
Thanks,
You can shorten the query using REGEXP, like this:
SELECT * FROM class
WHERE name REGEXP 'first|12'
OR father_name REGEXP 'first|12'
OR age REGEXP 'first|12';
Here's the reference:
REGEXP
And like you said, if you have an array like this,
$arr = array('first',12);
Then your query should be like this:
$query = "SELECT * FROM class WHERE name REGEXP '" . $arr[0] . "|" . $arr[1] . "' OR father_name REGEXP '" . $arr[0] . "|" . $arr[1] . "' OR age REGEXP '" . $arr[0] . "|" . $arr[1] . "'";
Related
so I have this query below in my php code :
$query ="SELECT *
FROM material_tools_master_data
WHERE material_name like '" . $_POST["keyword"] . "%'
ORDER BY material_code
LIMIT 0,50";
It does pretty well and give me a result called 'autocomplete' in my form. The problem is, I wanna make it more complex, I want my autocomplete filter the data selection not only by material_name but also with material_tools_group and show me exactly the material_name which is filtered by material_group = 'Measuring' OR 'Tools'.
The point is, I want to make this query works with my autocomplete. So here is my new query :
$query ="SELECT *
FROM material_tools_master_data
WHERE `material_tools_group` = 'Measuring' OR 'Tools' AND `material_name` like '" . $_POST["keyword"] . "%'
ORDER BY material_code LIMIT 0,50";
The query above is not working, the query above is giving me all the material_name rows in the table.
Any help will be much appreciated.
See warnings about PHP's deprecated API, and the proper use of prepared statements above...
$query ="
SELECT *
FROM material_tools_master_data
WHERE material_tools_group IN('Measuring','Tools')
AND `material_name` LIKE '" . $_POST["keyword"] . "%'
ORDER
BY material_code LIMIT 0,50;
";
Morning peps!
I'm having some troubles getting my query to work properly. I've created a search field which pulls rows from multiple tables (invoices, customers). For example, when ever I'm searching for invoice "2014-003", which is the only invoice with that nr., then I'm getting a list of all of my customers and then the same invoice nr. all the way down..
My code:
$results = $db->query("SELECT * FROM invoices, customers
WHERE invoice.customer_id = customers.id AND
(customers.name LIKE '%" . $search . "%') OR
(invoices.invoice_nr LIKE '%" . $search . "%') OR
(invoices.email LIKE '%" . $search . "%') OR
(customers.email LIKE '%" . $search . "%')");
I suck at english, so I think it's better for me to maybe write an example below:
I'll search for the invoice nr. "2014-003" which is unique, and what I get is this:
Peter - 2014-003
Christian - 2014-003
Adam - 2014-003
Frederick - 2014-003
Peter - 2014-003
and it should return:
Adam - 2014-003
because Adam is the only one who has an invoice with the following invoice nr.
I hope you'll understand what I want to do :)
Give this a whirl:
$results = $db->query("
SELECT
*
FROM
invoices,
customers
WHERE
invoice.customer_id = customers.id AND
((customers.name LIKE '%" . $search . "%') OR
(invoices.invoice_nr LIKE '%" . $search . "%') OR
(invoices.email LIKE '%" . $search . "%') OR
(customers.email LIKE '%" . $search . "%')
)");
Your ORs are not grouped properly, hence the AND validates, but is later relaxed because it gets a hit on one of the ORs - whereas your OR's are really referring to a group of possible conditions (but the original join needs to stay in place)
I'm working on a search module, like facebook when you look for a friend.
So, i have in my table user two fields : firstname and name.
If i have for example a user : firstname : Georges and name : Clooney, i want, when i write :
Geo...
Cloo...
loo geor...
geor ney
etc....
retrieve this user.
How can i do that with SQL ? I want to write a very permissive search module..
This will surely help you
Well the below method first calculate all possible combination of the possible words & then matches it with database
<?php
$name ='Georges Clooney'; // you search string
$words=explode(" ", $name);;
function get_all_combination($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0; $i<sizeof($arr);$i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1); // removes and returns the i'th element
if (sizeof($arrcopy) > 0) {
get_all_combination($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
$collect = array();
get_all_combination($words, "", $collect);
/*
$collect now have all possible combination of search string
Array
(
[0] => Georges
[1] => Georges Clooney
[2] => Clooney
[3] => Clooney Georges
)
*/
$sql="SELECT * FROM user_info WHERE (firstname like '%".implode("%' OR firstname like '%",$collect)."%' or name like '%".implode("%' OR name like '%",$collect)."%')" ;
?>
For any more help do ask
What you need, is this little Friend: %
WHERE firstname LIKE '%" . $name . "%' OR name LIKE '%" . $name ."%'
Here is a tutorial:
http://www.webreference.com/programming/php/search/index.html
Try This:
Select * from User where `name` LIKE '%search_string%' OR `firstname`
LIKE '%search_string%' OR CONCAT(firstname,' ',name) LIKE '%search_string'%'
SELECT *
FROM User
WHERE firstname LIKE '%'+#input+'%' OR name LIKE '%'+#input+'%'
where #input is the inserted text in the textbox.
Try this query:
SELECT * from user
WHERE name LIKE '%search_string%'
OR firstname LIKE '%search_string%'
It will match if any firstname or name containing search_string. it will show all users containing this string in name or first name.
Try this
select * from user where `name` like '%trim(search_string)%' OR `firstname`
like '%trim(search_string)%' OR CONCAT(firstname,' ',name) LIKE '%".trim(search_string)."%';
Also concat both the fields and try to match.
Say I have some records in my database with data like so:
John Murdoch
I am John Murdoch
My first name is john and my second name is murdoch
I have a search form and I type in "john murdoch" which will run this query:
$search //contain the search string in this case is john murdoch
$sql = mysqli_query($sql, "SELECT * FROM table WHERE column LIKE '%$search%'");
while($row = mysql_fetch_assoc($sql)){
echo $row['first']."<br>";
}
This will return the first two rows only because it is only those rows that have the both words beside each other. How can I return the other row even though the words are split up? I know I could explode the string and check each piece, but I was looking for a more stable and efficient way of doing this.
Just replace punctuation and spaces with the wildcard % before your query.
$search = str_replace ( array( '.', ' ', "'", '-' ), '%', $search );
This does still require the first word to appear in the text before the second word. So if your search was for "Murdoch John", you would not return any results.
The better answer is to employ FULLTEXT searching on the column (must do this in MySQL), and do a MATCH() query against the column, like so:
Add a plus sign before each word (to indicate that word is required)
$words = '+' . str_replace( ' ', ' +', $search );
And your query:
SELECT * FROM table MATCH ( column ) AGAINST ( '$words' IN BOOLEAN MODE)
More info here: http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html
SELECT * FROM table WHERE `column` LIKE '%john%' AND `column` LIKE '%murdoch%'
I would construct that query like this:
$search_terms = array('john','murdoch');
$field_to_search = 'some_column';
$sql = 'SELECT * FROM table WHERE' . "\n";
$sql .= '`' . $field_to_search . '` LIKE \'%'
. implode('%\' AND `' . $field_to_search . '` LIKE \'%',$search_terms)
. '%\'';
That PHP can be used with any number of search terms. It requires matches for all because it's connected with AND.
Here's a link to a php fiddle: http://phpfiddle.org/main/code/igv-3qc
I have 2 tables
members
=======
id
f_name // get these values.
l_name
email
friends
=======
to
from
The users's I'd is the value $member_id, if it is present in "to" or "from" I want it to not return the other value of that row, so only non-friends are shown.
Im creating a page to allow members to search through the database to add their friends by email, or name.
I'd like to return all rows from members where there is no record of the userid in either the "to" or "from" columns of the friends table.
I know how to do most basic mysql but joins are an issue for me, i don't really understand how to write them, if anyone can help me out and maybe even explain it a bit that would be great!
My query now:
$query = "SELECT * FROM members WHERE f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10";
//Where $word is the search term.
Modified Query
SELECT * FROM members WHERE id NOT IN (SELECT to FROM friends WHERE frm=$member_id) AND $member_id NOT IN (SELECT frm FROM friends WHERE to=$member_id) AND f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10
Edited
So, suppose a user with id 3:
SELECT *
FROM members
WHERE id <> 3
AND id NOT IN (SELECT to FROM friends WHERE from = 3)
AND id NOT IN (SELECT from FROM friends WHERE to = 3)
[Other conditions....];
OR
SELECT *
FROM members
LEFT JOIN friends
ON (members.id = friends.to
OR members.id = friends.from)
AND (members.to = 3
OR members.from = 3)
WHERE friends.to IS NULL
AND id <> 3
[Other conditions....];
Maybe consider change the name of your "from" and "to" columns, to avoid confusion, because FROM and TO are reserved words.
Also, you can use the reserved word EXPLAIN , before the query, to see the difference of the number of rows being fetched.
You might be able to do that using an LEFT Join, which will return all rows from members even if there's no corresponding row on the friend's table.
Something like this:
SELECT a.*, f.* FROM members m LEFT JOIN friends f
A great article to understand the many joins there are is this one.