2016-07-02 11 views
0

selectをいくつか作成していますが、サブクエリの値を使用してwhereステートメントを無視するように見える方法が見つかりませんでした。 私は1つのコンテンツテーブル、2つの他のテーブルを結合で2つの結合テーブル、もう1つのテーブルを結合wjth最初に直接持っていた。サブクエリを使用したMySql SELECTが期待どおりに動作しない

が、私はいくつかのソリューションを試みたが、これは1

select * from (SELECT a.idcontents, a.title, concat(a.filepath, '/', a.filename) as file, 
     a.captured_on, a.starred, a.file_type, 
     @PL:=(select GROUP_CONCAT(CONCAT(p.idplaces, '-', p.place) SEPARATOR ', ') from places p where a.idplaces=p.idplaces) as places, 
     @AL:=(select GROUP_CONCAT(CONCAT(al.idalbum, '-', al.album) SEPARATOR ', ') from album al, join_album_contents am where am.idalbum=al.idalbum and am.idcontents=a.idcontents) as album, 
     @PE:=(select GROUP_CONCAT(CONCAT(an.idpeople, '-', an.person) SEPARATOR ', ') from people an, join_people_contents ao where ao.idpeople=an.idpeople and ao.idcontents=a.idcontents) as people 
     FROM contents a) t 
     WHERE (@PE is null OR @AL is null OR @PL=1) 

最新のものですが、私は成功せず、あまりにも

SELECT a.idcontents, a.title, concat(a.filepath, '/', a.filename) as file, 
     a.captured_on, a.starred, CONCAT(p.idplaces, '-', p.place) as places, 
     (select GROUP_CONCAT(CONCAT(al.idalbum, '-', al.album) SEPARATOR ', ') from album al, join_album_contents am where am.idalbum=al.idalbum and am.idcontents=a.idcontents) as album, 
     (select GROUP_CONCAT(CONCAT(an.idpeople, '-', an.person) SEPARATOR ', ') from people an, join_people_contents ao where ao.idpeople=an.idpeople and ao.idcontents=a.idcontents) as people 
     FROM contents a, places p 
     WHERE a.idplaces=p.idplaces 
     and (@album IS NULL OR @people IS NULL or p.idplaces=1) 

これを試してみました、句は常に無視されているようで、結果は1かのレコードを含んでいますすべての部分が内部で句をとります。 レコードのみが正しく表示されます。

この問題を解決するソリューションがあるかどうかを知るだけです。

解決策は以下のコメントにあります!

t.people is null or t.album is null or t.places='1' 

ありがとう!!

+0

出力は、あなたが実際にそのサンプルに基づいて、クエリから取得するには、関連するテーブルごとにサンプルデータを入力してください、そしてどのようなあなたが得ることを望みます。 – trincot

+0

最初の質問で 't.peopleがヌルかt.albumがヌルかt.place = 1'と言うのはなぜですか?変数は必要ありません。 – shawnt00

+0

ありがとうshawnt00 !!!!それは作品です:)私は2日間このために失った!本当にありがとう – Dierre

答えて

0

はこの等価です:?

SELECT 
    a.idcontents, a.title, concat(a.filepath, '/', a.filename) as file, 
    a.captured_on, a.starred, a.file_type 
FROM contents a 
WHERE 
     NOT EXISTS (
      select 1 
      from people p inner join people_contents pc on pc.idpeople = p.people 
      where p.idpeople = a.idcontents /* is this the right column? */ 
     ) 
    or NOT EXISTS (
      select 1 
      from album al inner join album_contents ac on ac.idalbum = al.idalbum 
      where al.idcontents = a.idcontents 
     ) 
    or 1 = (
      select min(p.idplaces) from places p 
      where p.idplaces = a.idplaces 
      having count(*) = 1 
     ) 
関連する問題