0

我开发了一个在线拍卖系统,用户可以在其中销售或购买商品,我的问题是检索拍卖相关信息,这些信息位于两个单独的表格中,一个包含诸如此类(auction_id,owner,title,description,base_price,..)的信息,另一个包含有关每次拍卖请求的信息: (bid_id,auction_id,bidder,price,date),每个用户可以是否发布了几次拍卖,i want to show the highest price and the bidder(some one who gives such price) for that price and number of requests additional to information stored in auction table for each auction 但是当我加入表时,如果没有拍卖请求,那么结果将为零,您将看到消息:没有信息可显示,但用户刚刚发布了新的拍卖,什么我应该做吗?!我应该检查是否有每次拍卖的请求,如果有,然后获取这些信息?!代码重复?这样,我应该在一个配置文件页面请求中连接到 db 两次这是我的表和当前查询:

create table `auction`(
`auction_id`      INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`owner`            VARCHAR(32)  NOT NULL,
`group_id`         TINYINT      UNSIGNED NOT NULL ,
`title`            VARCHAR(100) NOT NULL,
`sale_type`        VARCHAR(1)   NOT NULL,
`base_price`       INT          NOT NULL,
`min_increase` INT NULL,
`photo`            VARCHAR(200) NULL,
`description`      VARCHAR(500) NOT NULL,
`start_date`       DATETIME     NOT NULL,
`termination_date` DATETIME     NULL,
`sold`             VARCHAR(1)   NOT NULL DEFAULT 0,
`purchaser`        VARCHAR(32)  NULL,
`deleted`          VARCHAR(1)   NOT NULL DEFAULT 0,
FOREIGN KEY(owner)     REFERENCES users(user_name) on delete cascade on update cascade,
FOREIGN KEY(purchaser) REFERENCES users(user_name) on delete cascade on update cascade,
FOREIGN KEY(group_id)  REFERENCES commodity_groups(group_id) on delete cascade on update cascade)
ENGINE=InnoDB default charset=utf8;

create table `bid`(
`bid_id`      INT         NOT NULL PRIMARY KEY AUTO_INCREMENT,
`auction_id`  INT  UNSIGNED        NOT NULL,
`bidder`      VARCHAR(32) NOT NULL,
`price`       INT         NOT NULL,
`date`        DATETIME    NOT NULL,
`deleted`     VARCHAR(1)  NOT NULL DEFAULT 0,
FOREIGN KEY(auction_id) REFERENCES auction(auction_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(bidder) REFERENCES users(user_name) ON DELETE CASCADE ON UPDATE CASCADE)
ENGINE=InnoDB default charset=utf8;    

这是我的查询,我使用准备好的语句:

SELECT `auction`.`auction_id` , `title` , `base_price` , `min_increase` , `photo` ,    `description` , `start_date` , `termination_date` , `max_bidder` , `bids_count` , `max_bid`
FROM `auction` , (

SELECT `bid`.`auction_id` , `bidder` AS max_bidder, `bids_count` , `max_bid`
FROM `bid` , (

SELECT `auction_id` , count( bid_id ) AS bids_count, max( price ) AS max_bid
FROM `bid`
WHERE `auction_id`
IN (

SELECT `auction_id`
FROM `auction`
WHERE `owner` = ?
)
GROUP BY (
auction_id
)
) AS temp
WHERE `bid`.`auction_id` = `temp`.`auction_id`
AND `price` = `max_bid`
) AS temp2
WHERE `auction`.`auction_id` = `temp2`.`auction_id`

很明显,如果没有拍卖请求,结果将为零,并且不会在他的个人资料中向用户显示拍卖,但是他或她刚刚发布了新的拍卖,如果有人可以帮助我,我将不胜感激

4

1 回答 1

2

您所拥有的更多的是数据库设计问题和未来的可伸缩性问题,而不是实际问题。你知道如果你愿意,你可以提出两个请求。

如果您关心扩大规模,您将不得不非常仔细地考虑您想要跨多个服务器复制哪些用户信息,以及您将如何同步这些信息。基本答案是:是的,您使用连接来包含您想要的用户信息。但更复杂的答案是,您可能希望创建仅包含一点用户信息(复制和同步)的迷你表,您可以非常快速地加入这些信息,任何用户都不会写入 - 换句话说,它们只被写入由主表通过从属设置或一些 cron 作业。

很大程度上取决于您希望您的网站有多大以及有多少人可能正在写入用户表。假设很多人将写入拍卖表,因此理想情况下,您不希望该表上有任何外键依赖项,否则您将遇到死锁。它可能应该是一个 ISAM 或联合表。

于 2013-11-01T22:02:20.517 回答