在查询时使用UNION运算符创建mysql视图

时间:2013-04-03 10:22:02

标签: mysql sql

我正在尝试使用UNION运算符创建sql视图。我先尝试执行sql,然后将其嵌入到视图中,结果就是成功。但是,当我尝试嵌入创建视图时,它返回了此错误

“错误代码:1064 您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'UNION附近使用正确的语法 SELECT p.product_media_id AS media_id,p.product_title AS title,p.product'at the line 8 “

我还保证所有列都具有相同的数据类型。

SQL视图:

CREATE
/*[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]*/
VIEW `mydbname`.`s_views` 
AS
(SELECT c.media_id AS media_id, 
    c.title AS title, 
    c.title_slug AS slug, 
    c.content_one      AS description, 
    c.type AS cat 
FROM content_content c 
WHERE c.type = 'news' 
    OR c.type='travel_genius' 
    AND c.media_id IS NOT NULL 
    AND c.status = 1
UNION
SELECT p.product_media_id AS media_id, 
    p.product_title AS title, 
    p.product_title_slug   AS slug, 
    p.product_description AS description, 
    'product' AS cat 
FROM p_views p
WHERE p.product_media_id IS NOT NULL);

1 个答案:

答案 0 :(得分:2)

这是MySQL中的一个错误。它是触发它的括号: -

http://bugs.mysql.com/bug.php?id=21614