我目前正在为在线游戏开发拍卖网站。但是,在创建统计信息时,我遇到了查询问题。
问题是我需要参加我参加的所有拍卖以及我丢失的拍卖。但到目前为止,我只看到能够得到所有丢失的拍卖(不仅仅是我参与的拍卖)。
假设我有以下表格:
表拍卖的表格结构
CREATE TABLE IF NOT EXISTS `Auction` (
`id` varchar(50) NOT NULL,
`preferedAuctionId` varchar(50) NOT NULL,
`winningTicketId` int(11) NOT NULL DEFAULT '-1',
`createdOn` datetime NOT NULL,
`startedOn` datetime NOT NULL,
`finishedOn` datetime NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-
AuctionBids
CREATE TABLE IF NOT EXISTS `AuctionBids` (
`bidId` varchar(50) NOT NULL,
`accountId` varchar(50) NOT NULL,
`auctionId` varchar(50) NOT NULL,
`ticketId` int(50) NOT NULL,
`datetime` datetime NOT NULL,
PRIMARY KEY (`bidId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
以下是我尝试的一些查询。
SELECT *
FROM Auction au, AuctionBids ab
WHERE au.id = ab.auctionId AND au.winningTicketId = ab.ticketId AND
ab.accountId NOT LIKE '". $_accountId ."' AND
au.finishedOn NOT LIKE '0000-00-00 00:00:00' AND au.active = '0'
和
SELECT *
FROM Auction
WHERE winningTicketId NOT IN
( SELECT ticketId
FROM AuctionBids
WHERE accountId = '". $_accountId ."')
AND finishedOn NOT LIKE '0000-00-00 00:00:00' AND active = '0'
如果您遗漏了任何重要信息,请询问我,我会为您提供。
你的真诚, Larssy1
在您的localhost上测试我已经进行了测试转储:
预期结果:
accountId'{0D3FB1B2-1C0C-06EA-979A-15F5B5231E7C}的人赢得了他参与的所有拍卖,这意味着他返回的行应为0。
accountId'{65CC1576-C3C3-5AC1-8098-B8CF1347F481}'的人失去了他参与的所有拍卖,这意味着他返回的行应为1。
答案 0 :(得分:1)
我认为你正在寻找一张联合表。我假设winsTicket字段包含获胜的票证ID。
SELECT *
FROM Auction JOIN AuctionBids ON Auction.id=AuctionBids.auctionId
WHERE Auction.active=0 AND
AuctionBids.accountId=yourAccountID AND
Auction.winningTicket<>AuctionBids.ticketId
也许这会有所帮助或让你以正确的方式前进?
===更新===
如果你这样做怎么办:
SELECT
Auction.id AS auctionID,
Auction.winningTicketId,
SUM( IF(Auction.winningTicketId=AuctionBids.ticketId, IF(AuctionBids.accountId=yourID,1,0 ),0) ) AS youWON,
SUM( IF(AuctionBids.accountId=yourID,1,0) ) AS nYouBID
FROM Auction JOIN AuctionBids ON Auction.id=AuctionBids.auctionId
WHERE
Auction.active=0
GROUP BY Auction.id
ORDER BY youWON ASC, nYouBID DESC, auctionID ASC
如果你是实际出价的话,如果你是赢家的话,这将为每个非主动拍卖提供一行两个bool。然后,您必须解析第一行,直到遇到youWON == 1.
答案 1 :(得分:1)
也许是这样?
SELECT distinct a.* FROM Auction a JOIN AuctionBids ab ON a.id = ab.auctionId
WHERE a.winningTicketId not in
(select ticketId from AuctionBids WHERE accountId = '". $_accountId ."'
and a.id = auctionId)
AND a.id in
(select auctionId from AuctionBids WHERE accountId = '". $_accountId ."')
AND a.active = '0'
AND finishedOn NOT LIKE '0000-00-00 00:00:00';
编辑。在SQLFiddle
中试用答案 2 :(得分:0)
如果您想要它用于您的帐户ID,那么为什么您使用了ab.accountId NOT LIKE'“。$ _accountId。”'?
I think you should use ab.accountId LIKE '". $_accountId ."'
答案 3 :(得分:0)
像这样的东西,你可以检索'赢','输','参与'(未经测试,但方法是对的,我猜)
SELECT 'win', A.*, B,*
FROM Auction A INNER JOIN AuctionBids B
ON A.id = B.auctionId
WHERE B.accountId = $me
AND B.active = 0
AND A.winningTicketId = B.bidID
UNION
SELECT 'lose', A.*, B,*
FROM Auction A INNER JOIN AuctionBids B
ON A.id = B.auctionId
WHERE B.accountId = $me
AND B.active = 0
AND A.winningTicketId != B.bidID
UNION
SELECT 'ing', A.*, B,*
FROM Auction A INNER JOIN AuctionBids B
ON A.id = B.auctionId
WHERE B.accountId = $me
AND B.active = 1