English 中文(简体)
结构复杂(Maybe Outer Joins)
原标题:Complex SQL (Maybe Outer Joins)

我有以下两个表格:

Reviews table
id
user_id
review
title
datetime

PeopleFollows table
id
user_id
following_user_id
datetime

我想提出1点询问,以便及时取得10大成果。 例如,如果我有以下数据:

审查表

1 user1  "This is my review" "title" 2011-01-10  
2 user1 "Another review" "title again" 2011-01-08  

后续表

1 user2 user1 2011-01-09 

我想取得这样的结果:

Review id=2  
People follows id=1
Review id = 1  

我认为,我这样做的唯一途径是单独提出X限额问题,然后将它们合并起来,以取得X结果。

I might need to explain a little more.

问题回答

如果你按日期对表格进行索引,并且按时间在UNION之前按时间顺序排列,你就不需要全扫描。

(SELECT "Review", id, datetime FROM Reviews ORDER BY datetime DESC LIMIT 10)
UNION 
(SELECT "People", id, datetime FROM PeopleFollows ORDER BY datetime DESC LIMIT 10)
ORDER BY datetime DESC
LIMIT 10

You can use UNION, but query won t be optimal:

(SELECT "Review", id FROM Reviews)
UNION
(SELECT "People", folowing_user_id FROM PeopleFollows)
ORDER BY datetime DESC
LIMIT 10

您不能在座标中使用适当的指数,可以全心全意。 因此,在你自己的风险下使用。

更好的想法——不要试图合并表格。 用于此目的的第三轴线。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签