我正在嘗試使用UNION運算符在其上創建sql視圖。在將它嵌入到視圖之前,我嘗試執行sql,結果是成功。但是,當我嘗試嵌入到創建視圖時,它返回了此錯誤在查詢中使用UNION運算符創建mysql視圖
「錯誤代碼:1064 您的SQL語法有錯誤;請檢查與您的MySQL服務器版本對應的手冊,以獲取要使用的正確語法在8號線 附近 'UNION SELECT p.product_media_id AS media_id,p.product_title標題,p.product'「
我還保證所有列具有相同的數據類型。
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);
爲什麼你周圍的'SELECT'聲明括號? – Taryn
我只是在創建視圖時使用sqlyog的格式。我用括號語法成功創建了很多視圖 – user1528706
您是否嘗試刪除括號以查看它是否可行? – Taryn