2012-03-30 5 views
5

ユニオンを使用して複数のクエリを1つの結果セットに結合するmysqlクエリがあります。クエリは完全に機能します。mysql - ユニオンを含むビューを作成できません

私は、エラーメッセージを取得するビューを作成するために、同じクエリを使用したい:完璧に動作

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hul' at line 2 

私のクエリは次のとおりです。

select hulaminloadnumber1,deliveryid,drop1customer from localjhb where hulaminloadnumber1>0 
union 
select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hulaminloadnumber2>0 
union 
select hulaminloadnumber3,deliveryid,drop1customer from localjhb where hulaminloadnumber3>0 
union 
select hulaminloadnumber4,deliveryid,drop1customer from localjhb where hulaminloadnumber4>0 
union 
select hulaminloadnumber5,deliveryid,drop1customer from localjhb where hulaminloadnumber5>0 

query results

私ビューを作成するクエリは次のとおりです。

create view View_LoadvsCustomer as (
select hulaminloadnumber1,deliveryid,drop1customer from localjhb where hulaminloadnumber1>0 
union 
select hulaminloadnumber2,deliveryid,drop1customer from localjhb where hulaminloadnumber2>0 
union 
select hulaminloadnumber3,deliveryid,drop1customer from localjhb where hulaminloadnumber3>0 
union 
select hulaminloadnumber4,deliveryid,drop1customer from localjhb where hulaminloadnumber4>0 
union 
select hulaminloadnumber5,deliveryid,drop1customer from localjhb where hulaminloadnumber5>0) 

これはphpMyAdminの中に以下のエラーが発生します。何の書式設定や照合の問題があってはならないので error message

クエリがひどく設計されたテーブルからすべてです。ユニオンクエリに対してビューを作成できますか?

アドバイスありがとうございます。

は素晴らしい週末を持って、 ライアン

+1

すべてのSELECTクエリは同じ条件 - 'hulaminloadnumber1> 0'を持ちます。それが正しいか? – Devart

+0

こんにちは@Devart、あなたは間違って、悪いコードをコピー申し訳ありません。質問を修正します。ただし、正しいコードで同じエラーが発生します。 – Smudger

+1

ありがとうございました - エラーについて - Ashwinの答えを見てください。それはそれを修正します。 – Devart

答えて

23

は、ビュー定義からかっこを削除します。このサーバーにヒットしましたbug

+0

ありがとうございましたAshwin Aありがとうございました – Smudger

+2

ありがとうございました!正確に問題と解決策。将来誰かの検索を手助けするために、これは私が取得したエラーです: 'ERROR 1064(42000):あなたはSQL構文に誤りがあります。あなたのMySQLサーバのバージョンに対応するマニュアルをチェックし、正しい構文が 'UNION DISTINCT SELECT ...'の近くで使用されるようにしてください。 – dkamins

関連する問題