2012-03-04 11 views
0

私はいくつかのテーブルのユーザーの記事を復元しています。テーブルstructionのような...php myaql union query with different uid

id | uid | imgae | 
1 | 1 | 1.jpg | 
2 | 1 |  | 
3 | 2 | 2.jpg | 
4 | 3 | 3.jpg | 
5 | 3 |  | 
6 | 3 | 4.jpg | 
7 | 4 |  | 
... 

は、それから私は、10結果の魔女image!=''

(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 
UNION 
(select * from my_table where image!='' order by rand() limit 1) 

そして、どのようにさらにクエリを作ることを取得します。 10件の結果が10件ありuida.uid!=b.uid)?コードが長すぎる...簡単な方法?ありがとう。

答えて

1

変更限度とすることによりグループ含まれます:あなたがいないのはなぜ

select * from my_table where image!='' order by rand() group by uid limit 10 
1

SELECT * FROM my_table 
WHERE image != '' 
ORDER BY RAND() 
LIMIT 10 

あなたがちょうどあなたができるDISTINCT uidをしたい場合:

SELECT DISTINCT uid, id, image FROM my_table 
WHERE image != '' 
ORDER BY RAND() 
LIMIT 10 

(MySQLの人々は私の記憶をリフレッシュします - DISTINCT uidを選択すると、MySQLはcorを選択します) id、image行に応答するか、対応する行が必ずしも得られないGROUP BYのようなものですか?