0

3 つのテーブルにクエリを実行すると、最後のテーブルには検索する余分な行があります。
これを試しました:

(SELECT 't1' AS tn, navid, clicks AS cc, tags 
FROM t1 
WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
AND tags LIKE '%cars%' )

UNION DISTINCT

(SELECT 't2' AS tn, navid, clicks AS cc, tags 
FROM t2 
WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
AND tags LIKE '%cars%' )

UNION DISTINCT

(SELECT 't3' AS tn, navid, title, clicks AS cc, tags 
FROM t3 
WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
AND tags LIKE '%cars%' ) 

titlet3でフィールドを取得したいのですが、
助けてください、ありがとう

4

3 に答える 3

2

NULL のような値で、最初の 2 つのサブクエリに追加するだけです。

(SELECT 't1' AS tn, navid, NULL as title, clicks AS cc, tags 
 FROM t1 
 WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
       AND tags LIKE '%cars%' )
UNION DISTINCT
(SELECT 't2' AS tn, navid, NULL as title, clicks AS cc, tags 
 FROM t2 
 WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
       AND tags LIKE '%cars%' )
UNION DISTINCT
(SELECT 't3' AS tn, navid, title, clicks AS cc, tags 
 FROM t3 
 WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
       AND tags LIKE '%cars%' ) 
于 2012-05-14T01:57:10.243 に答える
1
于 2012-05-14T01:59:00.660 に答える
1

UNION には同じ数の列が必要です。また、列名は最初のクエリに基づいているため、UNION のすべてのクエリで列に名前を付ける必要はありません。

(SELECT 't1' AS tn, navid, '' As title, clicks AS cc, tags 
FROM t1 
WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
AND tags LIKE '%cars%' )

UNION DISTINCT

(SELECT 't2', navid, '', clicks, tags 
FROM t2 
WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
AND tags LIKE '%cars%' )

UNION DISTINCT

(SELECT 't3', navid, title, clicks, tags 
FROM t3 
WHERE DATE_ADD(d, INTERVAL '0 8' DAY_HOUR) <= NOW() 
AND tags LIKE '%cars%' )
于 2012-05-14T01:59:00.693 に答える