0

如果我的 ID 存在于结果中并返回 TRUE,如何检查 MySQL 表,但这有点复杂,因为我想检查记录的 GROUP 而不是该 GROUP 中的最后一个,下面的代码用于表和查询'不起作用,我想检查我是否$session_user_id存在于 GROUP OFauction_bid_item_id但不检查最后一行,因为最后一行是获胜行。

CREATE TABLE `auction_bids` (   
    `auction_bid_id` int(11) NOT NULL AUTO_INCREMENT,   
    `auction_bid_user_id` int(11) NOT NULL,  
    `auction_bid_seller_id` int(11) NOT NULL,   
    `auction_bid_item_id` int(11) NOT NULL,   
    PRIMARY KEY (`auction_bid_id`) 
);

MySQL 查询:

SELECT * FROM auction_bids 
WHERE auction_bid_user_id = '$session_user_id' 
GROUP BY auction_bid_item_id;
4

2 回答 2

0

加入一个确定用户是否在该组中的子选择。

即,以下将为您提供出价和一个字段,说明是否找到了用户。

SELECT auction_bids.*, IF(Sub1.auction_bid_item_id IS NULL, 'User not found', 'User found')
FROM auction_bids 
LEFT OUTER JOIN
(
    SELECT DISTINCT auction_bid_item_id 
    FROM auction_bids 
    WHERE auction_bid_user_id = '$session_user_id'
) Sub1
ON auction_bids.auction_bid_item_id = Sub1.auction_bid_item_id
GROUP BY auction_bids.auction_bid_item_id

编辑 - 试试这个尝试忽略最新的出价

SELECT auction_bids.*, IF(Sub1.auction_bid_item_id IS NULL, 'User not found', 'User found')
FROM auction_bids 
LEFT OUTER JOIN
(
    SELECT DISTINCT auction_bids.auction_bid_item_id 
    FROM auction_bids 
    LEFT OUTER JOIN
    (
        SELECT auction_bid_item_id, MAX(auction_bid_id) AS LastBid
        FROM auction_bids
        GROUP BY auction_bid_item_id
    ) Sub2
    ON auction_bids.auction_bid_item_id = Sub2.auction_bid_item_id
    AND auction_bids.auction_bid_id =  Sub2.LastBid
    WHERE Sub2.auction_bid_item_id IS NULL
    AND auction_bid_user_id = '$session_user_id'
) Sub1
ON auction_bids.auction_bid_item_id = Sub1.auction_bid_item_id
GROUP BY auction_bids.auction_bid_item_id
于 2013-07-12T09:22:36.313 回答
0

像这样的东西?(用php)

$id_exists = false;

$id        = mysql_real_escape_string($_GET['id']);
$select    = "SELECT auction_bid_id FROM auction_bids WHERE auction_bid_id ='$id'";

$result    = mysql_query($select);

if(mysql_num_rows($result) > 0){
   $id_exists = true;
}else{
   $id_exists = false;
}
于 2013-07-12T09:29:09.993 回答