使用PHP的SQL语句 - 从每个供应商中选择最新添加的行

时间:2012-01-18 09:09:35

标签: php mysql sql select

这个问题可能很容易解决,但问题来了。

我想从每个供应商处获取最新数据(每个供应商插入数据库的最新行)。

这是来自MySQL的一些示例数据:

id  vendorId  vendor   url          delay   price0   price1   price2   price3   price4   price5   price6     date
1   1         vendor1  vendor1.com    0       12       15       18       14       25       28       25       2012-01-18 09:43:40
2   2         vendor2  vendor2.com    0       12       15       18       14       25       28       25       2012-01-18 09:43:40
3   1         vendor1  vendor1.com    0       15       17       122      12       30       52       53       2012-01-18 10:02:40
4   2         vendor2  vendor2.com    0       13       12       123      16       54       61       91       2012-01-18 10:02:40

我希望输出为:

id  vendorId  vendor   url          delay   price0   price1    price2   price3   price4   price5   price6     date
3   1         vendor1  vendor1.com    0       15       17       122       12       30       52       53       2012-01-18 10:02:40
4   2         vendor2  vendor2.com    0       13       12       123       16       54       61       91       2012-01-18 10:02:40

SQL是什么以及如何将值放入此数组?

$vendor = array(
       array('vendorId' => '1', 'vendor' => 'vendor1', 'url' => 'vendor1', 'delay' => 0, 'price0' => 12, 'price1' => 15, 'price2' => 26, 'price3' => 14, 'price4' => 25, 'price5' => 64, 'price6' => 512),
       array('vendorId' => '2', 'vendor' => 'vendor2', 'url' => 'vendor2', 'delay' => 0, 'price0' => 12, 'price1' => 15, 'price2' => 26, 'price3' => 14, 'price4' => 25, 'price5' => 64, 'price6' => 512)
);

提前致谢

另外,我忘了提到:数据库中不仅仅有2个供应商,所以我更希望每个汽车都能获得它们。

5 个答案:

答案 0 :(得分:1)

尝试此查询 -

SELECT v1.* FROM vendors v1
  JOIN (
    SELECT vendorId, MAX(date) date FROM vendors GROUP BY vendorId
  ) v2
ON v1.vendorId = v2.vendorId AND v1.date = v2.date

答案 1 :(得分:1)

SELECT max( id ) AS max_id, vendorId, url, delay, 
price0, price1, price2, price3, price4, price5, price6, date 
FROM `vendors` 
GROUP BY vendorId

您更新了如何将结果集获取到数组中的问题。 请参阅此处:http://php.net/manual/en/function.mysql-fetch-array.php

$row = mysql_fetch_array($result);

然后,您就可以将每一行作为关联数组添加到包含所有供应商的另一个数组中,可以通过键/值访问。

答案 2 :(得分:1)

不确定语法是否正确,但这大致是我要做的。

SELECT 
    `id`, `vendorId`, `vendor`, `url`, `delay`, `price0`, 
    `price1`, `price2`, `price3`, `price4`, `price5`, 
    `price6`, `date` 
FROM 
    `vendor_table_name` 
GROUP BY 
    `vendor_id` 
HAVING 
    `date` = max(`date`)

答案 3 :(得分:1)

尝试以下:

select * from vendor_table_name as ts 
left join 
(select max(id) as id from vendor_table_name group by vendorId) as tsm
on ts.id=tsm.id

然而,在50K-80K行之后它会很慢,它有限制

最好的方法是查看最新记录并使用视图加入。

如下所示:

  create view latestrecords as 
  select max(id) as id,vendorId from vendor_table_name group by vendorId

并加入视图:

  select * from vendor_table_name as ts 
    left join 
    latestrecords  as tsm
    on ts.id=tsm.id

答案 4 :(得分:-2)

试试这个

SELECT * FROM vendor_table_name ORDER BY date DESC

对于数组

$result =  (query result)

foreach($result as $r){
     $array = array("name" => $r->name, .....);
}