简体   繁体   English

三个表内部联接

[英]Three tables inner join

I have three tables "user" , "bidding" and "item". 我有三个表“ user”,“ bidding”和“ item”。 I need to find the query in order to get the completed item auctions for a buyer. 我需要找到查询才能为买家获得完成的物品拍卖。 The way how to find this in my database is the following, item.received=1 AND u.userid=X (this X will be filled in from my PHP which gives the userID of the highest bid). 如何在我的数据库中找到此方法的方式如下:item.received = 1和u.userid = X(将从我的PHP中填写此X,从而给出最高出价的userID)。 (note that received=1 implies that the deadline is over so this check is not necessary anymore). (请注意,received = 1表示截止日期已结束,因此不再需要进行此检查)。

Short explanation of the system: it is an auction website, where a user places bids on items and on the users personal account page I want to show the amount of auctions which he bought (and are processed, thus completed). 系统的简要说明:这是一个拍卖网站,用户在其中对商品和用户个人帐户页面进行出价,我想显示他购买(并处理,从而完成)的拍卖金额。

The 3 tables look like this: 3个表如下所示:

CREATE TABLE user (
    userid INT NOT NULL AUTO_INCREMENT,
    username CHAR(30)  NOT NULL UNIQUE,
    password CHAR(32)  NOT NULL,
    firstname CHAR(30)  NOT NULL,
    lastname CHAR(30)  NOT NULL,
    gender CHAR(1) NOT NULL,
    email CHAR(50)  NOT NULL UNIQUE,
    birthdate DATE  NOT NULL,
    addressid INT NOT NULL,
    picture CHAR(50),
    lastlogin TIMESTAMP NOT NULL,
    role CHAR(30),
    paymentid INT NOT NULL,

    PRIMARY KEY (userid),
    FOREIGN KEY (addressid)
     REFERENCES address(addressid),
    FOREIGN KEY (paymentid)
     REFERENCES payment(paymentid)
);

CREATE TABLE item (
    itemid INT NOT NULL AUTO_INCREMENT,
    name CHAR(40) NOT NULL,
    description CHAR(255) NOT NULL,
    originalpurchasedate DATE,
    deadline TIMESTAMP NOT NULL,
    minprice DOUBLE,
    received BOOLEAN NOT NULL,
    dateadded TIMESTAMP NOT NULL,
    openbidding BOOLEAN NOT NULL,
    categoryid INT NOT NULL,
    ownerid INT NOT NULL,

    PRIMARY KEY (itemid),
    FOREIGN KEY (categoryid)
     REFERENCES category(categoryid),
    FOREIGN KEY (ownerid)
     REFERENCES user(userid)
);

CREATE TABLE bidding (
    userid INT NOT NULL,
    itemid INT NOT NULL,
    amount DOUBLE,
    bidtime TIMESTAMP NOT NULL,

    FOREIGN KEY (userid)
     REFERENCES user(userid),
    FOREIGN KEY (itemid)
     REFERENCES item(itemid)
);

The malfunctioning solution I have already is: the result is 3 rows and results being: 3 , 1 , 5. The solution I expect to get only has to be 1 row, containing the number of distinct items. 我已经遇到的故障解决方案是:结果是3行,结果是:3,1,5。我希望得到的解决方案只能是1行,其中包含不同项目的数量。

SELECT DISTINCT COUNT(u.userid) FROM `item` i 
    INNER JOIN `bidding` b ON i.itemid = b.itemid
    INNER JOIN `user` u ON b.userid = u.userid
WHERE i.received=1 AND u.userid=2
GROUP BY i.itemid

You need to change your query to group on userid instead of item id, and count different items instead of different users. 您需要将查询更改为按用户ID(而非项目ID)分组,并计算不同的项目(而非不同的用户)。

SELECT DISTINCT COUNT(i.itemid) FROM `item` i 
    INNER JOIN `bidding` b ON i.itemid = b.itemid
    INNER JOIN `user` u ON b.userid = u.userid
WHERE i.received=1 AND u.userid=2
GROUP BY u.userid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM