Jquery UI 使用 UNION 查询从多个数据源自动完成

发布于 2024-11-25 15:47:54 字数 1381 浏览 2 评论 0原文

以下查询可以正常工作,将公司名称从一个表提取到 jquery UI 自动完成中:

SELECT name FROM company WHERE name LIKE "'. mysql_real_escape_string($_REQUEST['term']) .'%"

结果循环到一个数组中并编码为 JSON,然后返回到 jquery 并正确显示自动完成建议。

我正在尝试修改它以从两个或多个表中提取:

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']) .'%"

本例的目标是让自动完成列表包含公司名称和联系人姓名。当我使用示例搜索词独立于我的应用程序(仅使用 PhpMyAdmin 控制台)运行查询时,它成功显示了所需的结果。但是,在我的 jquery ui 自动完成表单的上下文中,它不会返回任何自动完成建议。

我将不胜感激任何建议。谢谢!

编辑:示例 SQL 结果

这是我在 PhpMyAdmin 中使用测试查询“mi”运行每个查询时得到的结果。

我原来的单表源查询:

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 建议的修改:

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 WHERE temptable.name LIKE "mi%" ; 
Rows: 15

name
[rows removed]

两者都是有效的 SQL,都会生成一个包含名称的行的一列表,但只有第一个可以在 jquery ui 中工作。 =(

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 WHERE 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. =(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山有枢 2024-12-02 15:47:54

请参阅下面的粗体文本。

SELECT temptable.[名称]
FROM (

选择名称 作为 [名称]
FROM 公司

UNION ALL

SELECT TRIM(ISNULL(fname,'') + ' ' + ISNULL(lname,'')) as [name]
来自联系人

) AS 诱人
哪里
temptable.name LIKE "'.mysql_real_escape_string($_REQUEST['term']) .'%"

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']) .'%"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文