2017-07-09 8 views
0

postsから12の投稿を選択します。 12人がいない場合は、parchiveテーブルから欠落を選択する必要があります。UNIONとORDER BYの不正な使用

$stmt = $db->query("SELECT * FROM posts where status='public' and user='public' order by inde asc limit 12 offset " . $offset); 
$count1 = $stmt->rowCount(); 
if ($count1 < 12){ 
    $diff = 12 - $count1; 
    $stmt = $db->query("SELECT * FROM posts where status='public' and user='public' order by inde asc limit 12 offset " . $offset . 
    " union select * from parchive where status='public' order by date desc limit " . $diff); 
} 

エラー:
General error: 1221 Incorrect usage of UNION and ORDER BY in...

任意のヘルプ?

答えて

2

あなたは、結果セットにこのように組合

$stmt = $db->query("(SELECT * 
       FROM posts 
       where status='public' and user='public' 
       order by inde asc limit 12 offset " . $offset . 
      ") union (select * 
      from parchive where status='public' order by date desc limit " . $diff .")"); 

を選択UNIONの仕事を中心に)(のカップルを追加しないで直接選択

に限ら必要と秩序組合のために選択した場合
+0

それは動作します。どうもありがとう。 – bonaca

関連する問題