从mysql查询中返回最新的10条记录

时间:2015-09-21 22:55:47

标签: php mysql

这是我的意思的链接 - i.stack.imgur.com/yYQbu.png。

从2列名称和版本查询。(可能只有一列我将版本放在名称列中)。 因此,Wheres匹配来自相同列名称的标题或帖子的名称,并返回版本(列)中的所有最小数字。

有人可以给我一些查询的例子吗?

到目前为止,我有这个问题:

   $name = substr($row[2],0,10);
   $q = mysql_query("SELECT name from films WHERE name LIKE '$name'") or die(mysql_error());

1 个答案:

答案 0 :(得分:1)

我假设您的表格中有一个{ "queryPlanner" : { "plannerVersion" : NumberInt(1), "namespace" : "stats.MailingStat", "indexFilterSet" : false, "parsedQuery" : { "$and" : [ { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } } ] }, "winningPlan" : { "stage" : "FETCH", "filter" : { "$and" : [ { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } } ] }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "startTime" : NumberInt(1) }, "indexName" : "startTime", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "startTime" : [ "[new Date(1442476800000), new Date(1442476800000)]" ] } } }, "rejectedPlans" : [ { "stage" : "FETCH", "filter" : { "$and" : [ { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } } ] }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "outlet_id" : NumberInt(1) }, "indexName" : "outlet_id", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "outlet_id" : [ "[ObjectId('542f1477ccc0b4ec548b471d'), ObjectId('542f1477ccc0b4ec548b471d')]" ] } } }, { "stage" : "FETCH", "filter" : { "$and" : [ { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } } ] }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "mailing_id" : NumberInt(1) }, "indexName" : "mailing_id_i", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "mailing_id" : [ "[ObjectId('55f2d561a5950e541b8e35fd'), ObjectId('55f2d561a5950e541b8e35fd')]" ] } } }, { "stage" : "FETCH", "filter" : { "$and" : [ { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } } ] }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "endTime" : NumberInt(1) }, "indexName" : "endTime", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "endTime" : [ "[new Date(1442480399999), new Date(1442480399999)]" ] } } }, { "stage" : "FETCH", "filter" : { "$and" : [ { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } }, { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } } ] }, "inputStage" : { "stage" : "AND_SORTED", "inputStages" : [ { "stage" : "IXSCAN", "keyPattern" : { "outlet_id" : NumberInt(1) }, "indexName" : "outlet_id", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "outlet_id" : [ "[ObjectId('542f1477ccc0b4ec548b471d'), ObjectId('542f1477ccc0b4ec548b471d')]" ] } }, { "stage" : "IXSCAN", "keyPattern" : { "startTime" : NumberInt(1) }, "indexName" : "startTime", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "startTime" : [ "[new Date(1442476800000), new Date(1442476800000)]" ] } } ] } }, { "stage" : "FETCH", "filter" : { "$and" : [ { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } }, { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } } ] }, "inputStage" : { "stage" : "AND_SORTED", "inputStages" : [ { "stage" : "IXSCAN", "keyPattern" : { "mailing_id" : NumberInt(1) }, "indexName" : "mailing_id_i", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "mailing_id" : [ "[ObjectId('55f2d561a5950e541b8e35fd'), ObjectId('55f2d561a5950e541b8e35fd')]" ] } }, { "stage" : "IXSCAN", "keyPattern" : { "startTime" : NumberInt(1) }, "indexName" : "startTime", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "startTime" : [ "[new Date(1442476800000), new Date(1442476800000)]" ] } } ] } }, { "stage" : "FETCH", "filter" : { "$and" : [ { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } }, { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } } ] }, "inputStage" : { "stage" : "AND_SORTED", "inputStages" : [ { "stage" : "IXSCAN", "keyPattern" : { "startTime" : NumberInt(1) }, "indexName" : "startTime", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "startTime" : [ "[new Date(1442476800000), new Date(1442476800000)]" ] } }, { "stage" : "IXSCAN", "keyPattern" : { "endTime" : NumberInt(1) }, "indexName" : "endTime", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "endTime" : [ "[new Date(1442480399999), new Date(1442480399999)]" ] } } ] } }, { "stage" : "FETCH", "filter" : { "$and" : [ { "mailing_id" : { "$eq" : ObjectId("55f2d561a5950e541b8e35fd") } }, { "outlet_id" : { "$eq" : ObjectId("542f1477ccc0b4ec548b471d") } }, { "endTime" : { "$eq" : ISODate("2015-09-17T08:59:59.999+0000") } }, { "startTime" : { "$eq" : ISODate("2015-09-17T08:00:00.000+0000") } } ] }, "inputStage" : { "stage" : "AND_SORTED", "inputStages" : [ { "stage" : "IXSCAN", "keyPattern" : { "mailing_id" : NumberInt(1) }, "indexName" : "mailing_id_i", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "mailing_id" : [ "[ObjectId('55f2d561a5950e541b8e35fd'), ObjectId('55f2d561a5950e541b8e35fd')]" ] } }, { "stage" : "IXSCAN", "keyPattern" : { "outlet_id" : NumberInt(1) }, "indexName" : "outlet_id", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "outlet_id" : [ "[ObjectId('542f1477ccc0b4ec548b471d'), ObjectId('542f1477ccc0b4ec548b471d')]" ] } } ] } } ] }, "serverInfo" : { "host" : "stat-web", "port" : NumberInt(27017), "version" : "3.0.2", "gitVersion" : "6201872043ecbbc0a4cc169b5482dcf385fc464f" }, "ok" : NumberInt(1) } 列作为自动增量的主键。

在这种情况下,您需要在查询中添加idORDER BY

LIMIT

$q = mysql_query("SELECT name from films WHERE name LIKE '$name' ORDER BY id DESC LIMIT 10") or die(mysql_error()); 会使您的查询首先放入最新记录。

ORDER BY id DESC将确保您只获取10条记录。