Jquery UI Autocomplete from multiple data sources using UNION query
The following query works properly to pull company names from one table into jquery UI autocomplete:
SELECT name FROM company WHERE name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%"
The result is looped into an array and encoded to JSON then returned to jquery and displays autocomplete suggestions properly.
I am trying to modify it to pull from two or more tables:
SELECT name
FROM (
SELECT name
FROM company
UNION ALL SELECT CONCAT( fname, ' ', lname )
FROM contact
) AS name
WHERE name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%"
The goal in this case would be to have the autocomplete list include company names and contact names. When I run the query independently of my application (just using PhpMyAdmin console) with a sample search term, it successfully displays the desired results. However in the context of my jquery ui autocomplete form, it does not return any autocomplete suggestions.
I would appreciate any suggestions. Thanks!
EDIT: Sample SQL results
Here is the result I get when I run each of these queries in PhpMyAdmin with test query "mi".
My original one-table source query:
Generation Time: Jul 20, 2011 at 01:40 AM
Generated by: phpMyAdmin 3.3.9 / MySQL 5.5.8
SQL query: SELECT name FROM company WHERE name LIKE "mi%" LIMIT 0, 30 ;
Rows: 6
name
[rows removed]
Mr. Wanda's suggested modification:
Generation Time: Jul 20, 2011 at 01:50 AM
Generated by: phpMyAdmin 3.3.9 / MySQL 5.5.8
SQL query: SELECT temptable.name FROM ( SELECT name as name FROM company UNION ALL SELECT CONCAT( fname, ' ', lname ) as name FROM contact ) AS temptable WH开发者_如何学运维ERE temptable.name LIKE "mi%" ;
Rows: 15
name
[rows removed]
Both are valid SQL that result in a table of one column with rows containing names, but only the first one works in jquery ui. =(
See bold text below.
SELECT temptable.[name]
FROM (
SELECT name as [name]
FROM company
UNION ALL
SELECT TRIM(ISNULL(fname,'') + ' ' + ISNULL(lname,'')) as [name]
FROM contact
) AS temptable
WHERE
temptable.name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%"
精彩评论