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;
";
Related
I have a SQL database with all my customer tickets that i access trough this link to show ALL tickets from selected customer:
...pull.php?user=customer
and with this query:
$query2x = 'SELECT * FROM `client_invoices` WHERE `username` = \'' . $user . '\' ORDER BY date_due ASC LIMIT ' . $i . ',' . $v;
I have 5 distinct ticket states, and i need to filter the above results by states, possibly in the link, like this:
...pull.php?user=customer&state=entry
...pull.php?user=customer&state=due
etc...
but if i add
`state` = \'' . $state. '\'
to the query this is possible but cant find a way to present all tickets in all states...
Any ideas to achieve this?
Thanks,
Sandro
I have a question for this, maybe I am doing the wrong way
as I have created a two different search function which they have own different function search
as one is SKU and one is Date
I have created a code for MySQL and Workbench and it working fine as I'm using
SELECT
settlement_id, sku
FROM
settlements
WHERE
sku LIKE 'ISCE%'
AND settlement_id LIKE '7072432852'
GROUP BY sku
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku
and it displayed fine
I use this same function for the PHP pages
$output = '';
if (isset($_POST["query"])) {
$search = mysqli_real_escape_string($conn, $_POST["query"]);
$queryskulist = "
SELECT
settlement_id,
sku,
FROM settlements
WHERE sku LIKE '%" . $search . "%' and settlement_id LIKE '%" . $search . "%'
GROUP BY sku
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku
";
} else {
$queryskulist = "
SELECT
settlement_id,
sku,
FROM settlements
GROUP BY sku
HAVING sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku ";
}
as you can see
WHERE sku LIKE '%" . $search . "%' and settlement_id LIKE '%" . $search . "%'
that's where I am getting an error as I am able to get data from only one function like this
WHERE sku LIKE '%" . $search . "%'
but I am unable to get two different search result, should I use two different query $_POST["query"]?
Any advice would be great, or Sample/Example demo PHP site would be great, it would help me a big picture on it.
You can fix this by using the following
SELECT
settlement_id,
sku
FROM
settlements
WHERE
(CONCAT(sku, settlement_id) LIKE '%" . $search . "%')
AND
sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY
sku
Alternatively you should be able to wrap in parenthesis
WHERE (sku LIKE '%" . $search . "%') OR (settlement_id LIKE '%" . $search . "%')
do the fact you have not aggregation function you should use where
SELECT
settlement_id
, sku
FROM settlements
WHERE sku LIKE 'ISCE%'
AND settlement_id LIKE '7072432852'
WHERE sku IS NOT NULL AND LENGTH(sku) > 0
ORDER BY sku
the use of group by without aggreagtion function is deprecated in SQL .. and in the most recent version of mysql is not allowed .. (by default)
using group by without aggregation function you could obtain unpredictable result
Thank for the update, I have fixed this issue as I have decided to remove 'Sku' and keep the rest of it, so here is what happened.
'Settlement_id' is used an options dropdown value on the PHP pages which they are pulling data from MySQL and the 'SKU' search box are using on the input box. and the input box is using Javascript Ajax Filters to get data which it displayed result from 'Settlement_Id' data
here is a demo
http://amazonsettlement.ishka.ie/MultiSelect/
I'm trying to put together 2 sql query one for name :
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."'ORDER BY id DESC";
and one to get $from and $to:
$sql = "SELECT * FROM register WHERE time BETWEEN ('$from') AND ('$to')";
Can you suggest how it is possible to combine these two query to show one name selected aswell and from and to for that name that is selected aswell as to just show all results for name selected without the from and to?
SELECT * FROM register WHERE time BETWEEN #from AND #to
UNION
SELECT * FROM register WHERE name LIKE #name ORDER BY id DESC
This can be done using UNION, or alternatively, you could use OR condition:
SELECT *
FROM register
WHERE name LIKE '" .$name ."'
OR time BETWEEN ('$from') AND ('$to')
ORDER BY id DESC;
SELECT * FROM register WHERE name LIKE '" .$name ."'
OR time BETWEEN ('$from') AND ('$to')
ORDER BY id DESC
You can use Union to combine the results of both queries
SELECT * FROM register WHERE name LIKE '" .$name ."'
union all
SELECT * FROM register WHERE time BETWEEN ('$from') AND ('$to')
Your question is unclear but still can try something like this
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."'
OR (time BETWEEN ('$from') AND ('$to')) ORDER BY id DESC";
This is what I was after, thanks to my Oracle programmer you know who you are, Thought I share:
$query = "SELECT * FROM register WHERE name LIKE '" .$name ."' and time between ifnull(timestamp('$from'),time) and ifnull(timestamp('$to'),time) ORDER BY id DESC";
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 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.