将同一个表上的多个MySQL查询合并为一个

时间:2015-03-31 21:51:43

标签: php mysql sql database wordpress

如何在同一个表上进行多个查询,选择不同的列?

如果它有帮助...所有查询在SQL语句的select部分都有一个公共列。他们都选择ID,然后选择特定的内容。

因此,每个查询都需要ID以及以下任一项:post_namepost_titlepost_excerpt

此外,如果它有助于简化事情。我试图在这些列中搜索广泛匹配和完全匹配。

所以在我的例子中,我将寻找:"地板完成","地板",或"完成"在以下列中:post_namepost_titlepost_excerpt。都在同一张桌子里。

我已尝试使用UNION完成此操作。

以下是我的疑问:

Array
(
    [broad] => Array
        (
            [floor] => Array
                (
                    [slugs] => SELECT `ID`, `post_name` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_name` LIKE '%floor%'
                    [titles] => SELECT `ID`, `post_title` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_title` LIKE '%floor%'
                    [excerpts] => SELECT `ID`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_excerpt` LIKE '%floor%'
                )

            [finish] => Array
                (
                    [slugs] => SELECT `ID`, `post_name` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_name` LIKE '%finish%'
                    [titles] => SELECT `ID`, `post_title` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_title` LIKE '%finish%'
                    [excerpts] => SELECT `ID`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_excerpt` LIKE '%finish%'
                )

        )

    [exact] => Array
        (
            [slugs] => SELECT `ID`, `post_name` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_name` LIKE '%floor-finish%'
            [titles] => SELECT `ID`, `post_title` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_title` LIKE '%floor finish%'
            [excerpts] => SELECT `ID`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_excerpt` LIKE '%floor finish%'
        )

    [combined] => ( SELECT `ID`, `post_name` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_name` LIKE '%floor-finish%' ) UNION ( SELECT `ID`, `post_name` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_name` LIKE '%floor%' ) UNION ( SELECT `ID`, `post_name` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_name` LIKE '%finish%' ) UNION ( SELECT `ID`, `post_title` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_title` LIKE '%floor finish%' ) UNION ( SELECT `ID`, `post_title` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_title` LIKE '%floor%' ) UNION ( SELECT `ID`, `post_title` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_title` LIKE '%finish%' ) UNION ( SELECT `ID`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_excerpt` LIKE '%floor finish%' ) UNION ( SELECT `ID`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_excerpt` LIKE '%floor%' ) UNION ( SELECT `ID`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND `post_excerpt` LIKE '%finish%' )
)

但是,上述结果很有意思。我似乎得到了所有正确的结果,除了每个结果值的键(应该是列的名称)始终保持不变。它始终为post_name,即使分配给它的值可能是post_titlepost_excerpt

所以每个结果都有IDpost_name。基本上他们的键是错误的,但值似乎是准确的。

我也尝试过这样的事情:

Array
        (
            [broad] => Array
                (
                    [floor] => SELECT `ID`, `post_name`, `post_title`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND ( `post_name` LIKE '%floor%' OR `post_title` LIKE '%floor%' OR `post_excerpt` LIKE '%floor' )
                    [finish] => SELECT `ID`, `post_name`, `post_title`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND ( `post_name` LIKE '%finish%' OR `post_title` LIKE '%finish%' OR `post_excerpt` LIKE '%finish%' )
                )

            [exact] => SELECT `ID`, `post_name`, `post_title`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND ( `post_name` LIKE '%floor-finish%' OR `post_title` LIKE '%floor finish%' OR `post_excerpt` LIKE '%floor finish%' )
            [combined] => SELECT `ID`, `post_name`, `post_title`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND ( `post_name` LIKE '%floor-finish%' OR `post_title` LIKE '%floor finish%' OR `post_excerpt` LIKE '%floor finish%' ) UNION (SELECT `ID`, `post_name`, `post_title`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND ( `post_name` LIKE '%floor%' OR `post_title` LIKE '%floor%' OR `post_excerpt` LIKE '%floor%' )) UNION (SELECT `ID`, `post_name`, `post_title`, `post_excerpt` FROM tps_3_posts WHERE `post_status` = 'publish' AND ( `post_name` LIKE '%finish%' OR `post_title` LIKE '%finish%' OR `post_excerpt` LIKE '%finish%' ))
        )

)

这更像是我想要完成的事情。我希望每个结果都包含IDpost_excerptpost_slugpost_title。如果没有匹配项,请使用空值显示密钥,或者只是完全不显示密钥。

第二次尝试的问题是它只需要在三个所需列中的一个中匹配。因此,如果它在post_excerpt中匹配而在其他位置匹配,则它仍然会从post_titlepost_name中提取值。因此使结果不准确。

我已经阅读了几个看起来类似的问题,但是大多数都没有真正明确的答案......或者......问题/答案更适合于SEPARATE表上的多个查询。 / p>

有关在同一桌面上组合多个MySQL查询的任何指导或建议吗?

通过方式...我正在使用"合并"在我的两个例子中作为我发送到数据库的最终查询。

所以再一次......如果列中没有匹配项,则将它们键显示为null或完全忽略结果中的键。

2 个答案:

答案 0 :(得分:2)

由于查询中的UNION语句,您收到了错误的“密钥”。您具有不同的键名称,但在联合查询的三个不同列之间具有兼容的列类型。数据库引擎只是从第一个查询中选择列名并将其用于所有这些列,而不是抛出错误:

id | post_name # <= column name in first query
1  | "my post"

UNION

id | post_title # <= column name is different, but type is compatible, so UNION succeeds
1  | "my post title"

UNION

id | post_excerpt # <= ditto
1  | "my post excerpt"

会导致:

id | post_name # <= column name from first query
1  | "my post"
1  | "my post title"
1  | "my post excerpt"

这就是你所经历的。

相反,你可以这样做:

id | post_name | post_title | post_excerpt
1  | "my post" | null       | null        # <= deliberately select nulls for these columns in this query

UNION

id | post_name | post_title      | post_excerpt
1  | null      | "my post title" | null

UNION

id | post_name | post_title | post_excerpt
1  | null      | null       | "my post excerpt"

这会给你带来如下结果:

id | post_name | post_title      | post_excerpt
1  | "my post" | null            | null
1  | null      | "my post title" | null
1  | null      | null            | "my post excerpt"

使用您的表格,其基本版本可能如下所示:

SELECT ID, post_name, null AS post_title, null AS post_excerpt FROM tps_3_posts

UNION

SELECT ID, null AS post_name, post_title, null AS post_excerpt FROM tps_3_posts

UNION

SELECT ID, null AS post_name, null AS post_title, post_excerpt FROM tps_3_posts

可能更适合您尝试的操作。 Here's a SQLFiddle如果你想看到它的实际效果。

答案 1 :(得分:0)

正如我在第二种情况下所理解的那样,问题是只有匹配的列应该有值而其他列应该为null。对于这种情况,您可以在SELECT中使用IF / ELSE或WHEN语句。我们的想法是选择实际值,此列的值对应于预期值,否则为null。

另一种可能的解决方案是你可以写出例如。

SELECT `ID`, `post_name`, `post_title`, 'post_excerpt'

因此,您选择的字符串值是WHERE语句中的条件,稍后可以使用此值来了解此行所属的内容。