MySQL与SELECT MAX相反

时间:2012-12-29 00:17:58

标签: mysql max

我正在尝试查找数据库中非MAX(bid_ts)的所有行。我可以使用!MAX(bid_ts)还是有更好的方法吗?

$auctionswinning = "
  SELECT 
    listings.end_date, 
    listings.user_id, 
    listings.title, 
    listings.auc_fp, 
    listings.id, 
    listings.auc_image1 
  FROM listings 
  INNER JOIN (
    SELECT
     listing_id, 
     user_id, 
     !MAX(bid_ts) maxDate
    FROM bids
    GROUP BY listing_id, user_id
  ) bids ON listings.id = bids.listing_id
  WHERE 
   bids.user_id=$userid
   AND listings.end_date > NOW()";

3 个答案:

答案 0 :(得分:1)

试试这个

SELECT 
    listings.end_date, 
    listings.user_id, 
    listings.title, 
    listings.auc_fp, 
    listings.id, 
    listings.auc_image1 
FROM listings 
INNER JOIN (SELECT
                 listing_id, 
                 user_id, 
                 bid_ts maxDate
            FROM bids
            WHERE bid_ts NOT IN (SELECT MAX(bid_ts) FROM bids)
            GROUP BY listing_id, user_id
            ) bids ON listings.id = bids.listing_id
WHERE bids.user_id=$userid
AND listings.end_date > NOW()

答案 1 :(得分:0)

你必须为此创建一个子查询:

SELECT bid_ts FROM tbl WHERE bid_ts <> (SELECT MAX(bid_ts) FROM tbl);

答案 2 :(得分:-1)

SELECT bid_ts
FROM tbl
HAVING bid_ts <> MAX(bid_ts)

我没有尝试过,最好避免HAVING,但它应该让你开始。