mySQL 从不同表中选择列

发布于 2024-11-07 09:19:44 字数 322 浏览 0 评论 0原文

1000 抱歉,如果我重复了一个问题,但在此处找不到我的问题的答案。

我尝试从同一查询中的 2 个不相关表的 2 个独立列中检索数据。

我尝试过使用 UNION 语句,但问题是我需要能够将结果分为“场地”和“节目” - 这就是我所做的:

SELECT venue_name
FROM my_venues
UNION
SELECT programme_title
FROM my_programmes;

也许没有必要合并查询,我可以只做 2 个单独的查询吗?数据库不会特别大,但似乎没有必要......

帮助和感谢!

1000 Apologies if I've repeated a question, couldn't find an answer here to my question.

I'm try to retrieve the data from 2 separate columns from 2 unrelated tables in the same query.

I've tried using a UNION statement, but the problem is that I need to be able to separate the results into 'venues' and 'programmes' - here was what I did:

SELECT venue_name
FROM my_venues
UNION
SELECT programme_title
FROM my_programmes;

Maybe it's not necessary to combine the query and I can just do 2 separate queries? The database won't be especially large, but it seems unnecessary...

Help and thanks!

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

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

发布评论

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

评论(1

千鲤 2024-11-14 09:19:44

只需在两个选择中添加一个具有相同名称但不同值的常量列:

SELECT "venues"  as source, venue_name as thing_name
FROM my_venues
UNION ALL
SELECT "programmes"  as source, programme_title as thing_name
FROM my_programmes;

现在:

  • 列的值为“venues”的行
    source 将来自表格
    my_venues
  • 值为“programmes”的行
    source将来自表
    我的程序

Just add a constant column in both selects, with the same name, but different values:

SELECT "venues"  as source, venue_name as thing_name
FROM my_venues
UNION ALL
SELECT "programmes"  as source, programme_title as thing_name
FROM my_programmes;

Now:

  • Rows with value "venues" for column
    source will come from the table
    my_venues ,
  • rows with value "programmes" for
    column source will come from table
    my_programmes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文