如果我的ID存在于结果中,请检入MySQL表

时间:2013-07-12 09:14:29

标签: mysql sql

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

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;

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
}