有一个帖子的回复表,
posts(id , tid , subject ,message ,dateline),
其中:id为自动增长字段,tid为该回复的主体帖子的id(外键关联),subject为回复标题,message为回复内容,dateline为回复时间,用UNIX时间戳表示。
请写SQL,
选出按时间排序的前十个来自不同主题的最新回复;
随机选一条回复记录;
黄舟2017-04-17 14:56:51
Visual inspection of the first sentence on the first floor is wrong. There is no guarantee of different topics. Here is what I wrote:
1 |
|
In fact, dateline and id should be in the same order. You can consider changing to id. The primary key on id should be more efficient:
1 |
|
In the second sentence, I personally think it is better not to use ORDER BY RAND(). A slightly larger amount of data will be inefficient, because writing this way may lead to multiple scans. It may be better to write it this way:
1 |
|
PHP中文网2017-04-17 14:56:51
Try it. If the table data is too large, it is not recommended to use ORDER BY RAND()
. Please try these two sentences SQL
first to see if they work. Hope it helps you.
1 2 3 |
|
怪我咯2017-04-17 14:56:51
The first answer upstairs is not correct by visual inspection. The following is my personal opinion. Comments and advice are welcome
1 |
|