0

我怎样才能使这样的工作?

INSERT INTO age.page(domain,title_count,youtube_count,ipaddress,updated)
SELECT * FROM 
(
    SELECT domain,
    COUNT(domain) AS titlecount,
   (SELECT COUNT(*) FROM table2 WHERE title = table1.title) AS YoutubeCount, ipaddress
    NOW() AS timeNow
    FROM table1
    GROUP BY domain
    ORDER BY title DESC
) a;

我想使用子查询来获取不同表的计数,但使用主查询中的相同字段。

我想这样做的原因是我不必运行两个查询,而是只运行一个。

4

1 回答 1

1

您可以COUNT在子查询中执行此操作,然后JOIN在第一个表中执行此操作:

INSERT INTO age.page(domain, title_count, youtube_count, ipaddress, updated)
SELECT * FROM 
(
    SELECT 
      domain,
      COUNT(domain) AS titlecount,
      t2.titlecount AS YoutubeCount, 
      ipaddress,
      NOW() AS timeNow
    FROM table1
    INNER JOIN
    (
       SELECT title, COUNT(*) Titlecount
       FROM table2 
       GROUP BY title
    ) AS t2 ON t2.title = table1.title   
    GROUP BY domain
    ORDER BY table1.title DESC
) a;
于 2013-03-19T19:59:36.917 回答