开发者

How to select second table in a query?

开发者 https://www.devze.com 2023-02-23 01:52 出处:网络
I am using this to get customer name for auto complete function. $query = $db->query(\"SELECT orderr_customer_name FROM orderr WHERE orderr_customer_name LIKE \'$queryString%\' GROUP by orderr_cus

I am using this to get customer name for auto complete function.

$query = $db->query("SELECT orderr_customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%' GROUP by orderr_customer_name LIMIT 10");
                if($query) {
                    while ($result = $query ->fetch_object()) {

                        echo '<li onClick="fill(\''.$result->orderr_customer_name.'\');">'.$result->orderr_customer_n开发者_JS百科ame.'</li>';

How can i Select another table? this is the table i want to use

("SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 10");

Thanks alot.


You should look into something like that (Note that I can't test it now, this is only a guideline):

SELECT orderr.orderr_customer_name, customer.customer_name 
FROM customer INNER JOIN orderr_customer_name ON orderr.orderr_customer_name = customer.customer_name 
WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 1

Pleasy read the doc: Here


It sounds like you're asking how to query and combine the results from two tables through the use of a single query. This can be accomplished rather easily with a UNION in MySQL.

(SELECT orderr_customer_name AS customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%')
UNION
(SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%')
ORDER BY customer_name LIMIT 10

Don't forget to make sure you escape/sanitize $queryString, and that you have proper indexes on your tables. Also, in your specific example, it appears you might be duplicating customer names in at least one place; while some applications require this, you may also want to consider normalization - but that's another topic.

0

精彩评论

暂无评论...
验证码 换一张
取 消