从多个表自动完成

发布于 2024-10-18 10:50:15 字数 557 浏览 1 评论 0原文

我想知道从多个表获取自动完成结果的最有效方法是什么?

需要注意的是;我希望能够识别记录来自哪个表。

例如,给定这些示例表:

+- People
   +- id
   +- name
   +- age

+- Places
   +- id
   +- name
   +- distance

+- Things
   +- id
   +- name
   +- color

虽然包含一些与此问题相关的任意数据,但这里的主要焦点是 name 列(尽管这些可能是其他内容,或者每个正在查询的表有多个)

无论如何,我我正在尝试弄清楚如何通过自动完成功能查询每个表中的数据,并返回数据以及它所在的表。例如,字母 A 的输出可能如下所示:

Apple (Things)
Amy (People)
Aaron (People)
Anaheim (Places)
Axe (Things)

这最好通过单个查询还是跨各个表的多个查询来完成?

提前致谢 :)

I'm wondering what is the most efficient way to obtain autocomplete results from multiple tables?

The caveat being; I want to be able to identify from what table the records are coming.

For example, given these example tables:

+- People
   +- id
   +- name
   +- age

+- Places
   +- id
   +- name
   +- distance

+- Things
   +- id
   +- name
   +- color

While containing some data arbitrary to this issue, the main focus here is the name columns (though these could be something else, or multiple per table being queried)

Anyways, I'm trying to figure out how to query each of these tables for data a' la autocomplete, and return the data and from what table it is. For example, the output for the letter A may look something like this:

Apple (Things)
Amy (People)
Aaron (People)
Anaheim (Places)
Axe (Things)

Would this be best accomplished with a single query, or multiple queries across the various tables?

Thanks in advance :)

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

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

发布评论

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

评论(1

隔岸观火 2024-10-25 10:50:15

您可以在单个查询中使用 at union 来完成此操作:

SELECT name, 'Things' as source FROM Things WHERE name LIKE 'A%'
UNION ALL
SELECT name, 'People' as source FROM People WHERE name LIKE 'A%'
UNION ALL
SELECT name, 'Places' as source FROM Places WHERE name LIKE 'A%'

现在您将获得匹配的名称和源表,并且您可以为自动完成下拉列表很好地格式化它。

You could do it with at union, in a single query:

SELECT name, 'Things' as source FROM Things WHERE name LIKE 'A%'
UNION ALL
SELECT name, 'People' as source FROM People WHERE name LIKE 'A%'
UNION ALL
SELECT name, 'Places' as source FROM Places WHERE name LIKE 'A%'

Now you would get both the matching name and the source table, and you could format that nicely for your auto-complete drop down thingie.

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