0

テーブル

id    string
------------
1     aaa
2     bbb
3     ccc
4     ddd

クエリ

(SELECT string FROM table WHERE id > 1 ORDER BY id ASC LIMIT 1)    /* num_row = 1 */
UNION
(SELECT string FROM table WHERE id < 1 ORDER BY id DESC LIMIT 1)   /* null */
UNION
(SELECT string FROM table WHERE id > 4 ORDER BY id ASC LIMIT 1)    /* null */
UNION
(SELECT string FROM table WHERE id < 4 ORDER BY id DESC LIMIT 1)   /* num_row = 2 */

2 rowsid=5とid=0がないため、上記のクエリが返されます。

これらの2つの行がどのクエリからフェッチされているかをどのように知ることができますか?

つまり、num_row = 1から1st SELECT、およびnum_row = 2から4th SELECT

4

4 に答える 4

2

あなたは試すことができます

(SELECT 1, string FROM table WHERE id > 1 ORDER BY id ASC LIMIT 1)   
UNION
(SELECT 2, string FROM table WHERE id < 1 ORDER BY id DESC LIMIT 1)  
UNION
(SELECT 3, string FROM table WHERE id > 4 ORDER BY id ASC LIMIT 1)   
UNION
(SELECT 4, string FROM table WHERE id < 4 ORDER BY id DESC LIMIT 1) 
于 2012-11-05T14:59:07.117 に答える
0

共通のエイリアスを持つ定数列を追加できます。

(SELECT string, 'query_1' as query_num FROM table WHERE id > 1 ORDER BY id ASC LIMIT 1)    
UNION
(SELECT string, 'query_2' as query_num FROM table WHERE id < 1 ORDER BY id DESC LIMIT 1)
UNION
(SELECT string, 'query_3' as query_num FROM table WHERE id > 4 ORDER BY id ASC LIMIT 1)    
UNION
(SELECT string, 'query_4' as query_num FROM table WHERE id < 4 ORDER BY id DESC LIMIT 1)   
于 2012-11-05T14:59:13.927 に答える
0

2番目の列を使用して、データの出所を示します

(SELECT string, '1st query' as from_where FROM table WHERE ...)
UNION
(SELECT string, '2nd query' as from_whereFROM table WHERE ...)
于 2012-11-05T14:59:52.607 に答える
0

あなたは試すことができます(そのままでは不法です):

(SELECT '1'、文字列FROMテーブルWHERE id> 1 ORDER BY id ASC LIMIT 1)/ * num_row = 1 / UNION(SELECT '2'、文字列FROMテーブルWHERE id <1 ORDER BY id DESC LIMIT 1)/ null / UNION(SELECT '3'、文字列FROMテーブルWHERE id> 4 ORDER BY id ASC LIMIT 1)/ null / UNION(SELECT '4'、文字列FROMテーブルWHERE id <4 ORDER BY id DESC LIMIT 1)/ num_row = 2

于 2012-11-05T15:00:21.397 に答える