从两个表中选择值

发布于 2024-12-11 04:31:26 字数 959 浏览 0 评论 0原文

我有两个表,ab。两个表都包含一个 config_data 列。 a 位于一个数据库中,b 位于另一个数据库中。

我想从 a 中选择 b 中没有的值,以及 b不是a中。

这是我当前的查询:

SELECT b.dim2 
FROM sakthi_a1.`config_data` AS a 
JOIN sakthi_a2.`config_data` AS b 
    ON a.dim2<>b.dim2 
GROUP BY b.dim2

它不起作用。我需要一个查询来解决这个问题。

忘记上面的查询吧。这是错误的,我知道。

我需要根据上面给出的条件从这些表中选择一个值。是否可以?

示例数据

sakthi_a1.config_data
id  dim1 dim2                    config_value
1   site is_module_page          0
2   site default_html_header_tpl html_headedr.tpl
3   site facebook                test


sakthi_a2.config_data
id  dim1 dim2                    config_value
1   site is_module_page          0
2   site default_html_header_tpl html_headedr.tpl
3   site error                   Invalid page

I have two tables, a and b. Both tables contain a config_data column. a is in one database and b in another.

I want to select values from a that are not in b, as well as values that are in b but not in a.

This is my current query:

SELECT b.dim2 
FROM sakthi_a1.`config_data` AS a 
JOIN sakthi_a2.`config_data` AS b 
    ON a.dim2<>b.dim2 
GROUP BY b.dim2

It's not working. I need a query to solve this problem.

Just forget about the above query. It's wrong, I know.

I need to select a value from those tables based on the conditions given above. Is it possible?

Sample data

sakthi_a1.config_data
id  dim1 dim2                    config_value
1   site is_module_page          0
2   site default_html_header_tpl html_headedr.tpl
3   site facebook                test


sakthi_a2.config_data
id  dim1 dim2                    config_value
1   site is_module_page          0
2   site default_html_header_tpl html_headedr.tpl
3   site error                   Invalid page

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

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

发布评论

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

评论(2

李白 2024-12-18 04:31:26

像下面的查询怎么样:

(
    SELECT
        a.*
    FROM
        a
    WHERE NOT EXISTS(
        SELECT
            *
        FROM
            b
        WHERE
            b.dim = a.dim
    )
)

UNION

(
    SELECT
        b.*
    FROM
        b
    WHERE NOT EXISTS(
        SELECT
            *
        FROM
            a
        WHERE
            a.dim = b.dim
    )
)

What about something like the following query:

(
    SELECT
        a.*
    FROM
        a
    WHERE NOT EXISTS(
        SELECT
            *
        FROM
            b
        WHERE
            b.dim = a.dim
    )
)

UNION

(
    SELECT
        b.*
    FROM
        b
    WHERE NOT EXISTS(
        SELECT
            *
        FROM
            a
        WHERE
            a.dim = b.dim
    )
)
亽野灬性zι浪 2024-12-18 04:31:26

试试这个(a中的行不在b中)

SELECT a.*
FROM sakthi_a1.`config_data` AS a 
LEFT JOIN sakthi_a2.`config_data` AS b 
    ON a.id = b.id
WHERE b.id IS NULL

Try this (rows in a that are not in b)

SELECT a.*
FROM sakthi_a1.`config_data` AS a 
LEFT JOIN sakthi_a2.`config_data` AS b 
    ON a.id = b.id
WHERE b.id IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文