12

なぜ#1222-使用されているSELECTステートメントの列数が異なるのですか?私はこのユーザーの友達と彼自身から壁の投稿をロードしようとしています。

SELECT u.id AS pid, b2.id AS id, b2.message AS message, b2.date AS date FROM 
(
    (
        SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 
        wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
        WHERE f.buddy_id = '1' AND f.status = 'b'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    UNION
    (
        SELECT * FROM
        wall_posts
        WHERE pid = '1'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    ORDER BY date DESC
    LIMIT 0, 10
) AS b2 
JOIN Users AS u
ON b2.pid = u.id
WHERE u.banned='0' AND u.email_activated='1'
ORDER BY date DESC
LIMIT 0, 10

wall_postsテーブル構造は次のようになりますid date privacy pid uid message

Friendsテーブルの構造は次のようになりますFid id buddy_id invite_up_date status

pidはプロファイルIDを表します。何が起こっているのかよくわかりません。

4

5 に答える 5

26

UNIONの最初のステートメントは、次の4つの列を返します。

SELECT b.id AS id, 
       b.pid AS pid, 
       b.message AS message, 
       b.date AS date 
  FROM wall_posts AS b 

*が展開されて:からのすべての列が含まれるため、 2番目のものは6を返します。WALL_POSTS

SELECT b.id, 
       b.date, 
       b.privacy,
       b.pid. 
       b.uid message
  FROM wall_posts AS b 

UNIONおよび演算子には、次のUNION ALLものが必要です。

  1. UNIONのクエリを構成するすべてのステートメントに同じ数の列が存在します
  2. データ型は、各位置/列で一致する必要があります

使用する:

FROM ((SELECT b.id AS id, 
             b.pid AS pid, 
             b.message AS message, 
             b.date AS date 
        FROM wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
       WHERE f.buddy_id = '1' AND f.status = 'b'
    ORDER BY date DESC
       LIMIT 0, 10)
      UNION
      (SELECT id,
              pid,
              message,
              date
         FROM wall_posts
        WHERE pid = '1'
     ORDER BY date DESC
        LIMIT 0, 10))
于 2010-10-15T22:55:04.107 に答える
4

UNION4列の関係(、、、、およびid)と6列の関係(=の6列)を使用しています。SQLではそれができません。pidmessagedate*wall_posts

于 2010-10-15T22:55:01.863 に答える
3
(
        SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 
        wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
        WHERE f.buddy_id = '1' AND f.status = 'b'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    UNION
    (
        SELECT id, pid  , message , date  
        FROM
        wall_posts
        WHERE pid = '1'
        ORDER BY date DESC
        LIMIT 0, 10
    )

最初のクエリで 4 を選択し、2 番目のクエリで 6 を選択していたので、それらを一致させます。

于 2010-10-15T22:56:52.813 に答える
0

MySQL Union を使用しています。

UNION is used to combine the result from multiple SELECT statements into a single result set.

The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)

参考:MySQL ユニオン

最初の select ステートメントには 4 つの列があり、wall_post には 6 つの列があると言ったように、2 番目のステートメントには 6 つの列があります。両方のステートメントで同じ数の列と同じ順序にする必要があります。そうしないと、エラーまたは間違ったデータが表示されます。

于 2019-12-03T07:08:12.980 に答える