2012-09-09 44 views
0

这是我的数据库SQL代码;SQL查询从当前日期检索字段

CREATE TABLE ProductType 
(
    ptID int not null auto_increment, 
    pType varchar(30), 
    PRIMARY KEY(ptID) 
)ENGINE=InnoDB; 

CREATE TABLE Service 
(
    sID int not null auto_increment, 
    description varchar(30) not null, 
    revenue int, 
    stID int not null, 
    PRIMARY KEY(sID), 
    FOREIGN KEY(stID) references ServiceType(stID) 
)ENGINE=InnoDB; 

CREATE TABLE Product 
(
    pID int not null auto_increment, 
    model varchar(30) not null, 
    cogs int, 
    ptID int not null, 
    PRIMARY KEY(pID), 
    FOREIGN KEY(ptID) references ProductType(ptID) 
)ENGINE=InnoDB; 

CREATE TABLE Sale 
(
    saleID int not null auto_increment, 
    assetID int not null, 
    eID int not null, 
    sID int not null, 
    pID int, 
    saDate date not null, 
    PRIMARY KEY(saleID), 
    FOREIGN KEY(eID) references Employee(eID), 
    FOREIGN KEY(sID) references Service(sID), 
    FOREIGN KEY(pID) references Product(pID) 
)ENGINE=InnoDB; 

CREATE TABLE Employee 
(
    eID int not null auto_increment, 
    firstName varchar(30) not null, 
    lastName varchar(30) not null, 
    phone varchar(30), 
    email varchar(30), 
    PRIMARY KEY(eID) 
) 

这是我的尝试,但我不知道如何从“服务”当我查询“出售”表访问“收入”字段。

<?php 
// Make a MySQL Connection 
mysql_connect("localhost", "root", "root") or die(mysql_error()); 
mysql_select_db("pnl") or die(mysql_error()); 

// Retrieve all the data from the "example" table 
$result = mysql_query("SELECT * FROM Sale WHERE saDate = CURDATE()") 
or die(mysql_error()); 

// store the record of the "example" table into $row 
$row = mysql_fetch_array($result); 
// Print out the contents of the entry 

echo "revenue: ".$row['revenue']; 

?> 

谁能给我的,我怎么会写一个PHP脚本来检索所有的销售收入为当前日期的例子吗?这也是我第一次尝试SQL和关系数据库模型,所以如果你发现我犯的任何重大错误或者我可以改进的地方,请告诉我!

+0

哪个列引用您的销售表中的价格? – David

+0

尝试进行编辑。 @David我以为我可以通过FK服务(sID)从服务中获得收入。我错在认为? – pjmil

+0

试试这个。我不确定我是否了解数据的工作原理。 SELECT SUM(Service.revenue)FROM服务JOIN销售Sale.SID = Service.sID WHERE Sale.saDate ='您的日期格式' – David

回答

1

试试这个方法。命名SUM字段并按给定名称访问它:

// Retrieve all the data from the "example" table 
$result = mysql_query("SELECT SUM(Service.revenue) sum FROM Service JOIN Sale ON Sale.sID = Service.sID WHERE Sale.saDate = $some_date") or die(mysql_error()); 
// store the record of the "example" table into $row 
$row = mysql_fetch_array($result); 
// Print out the contents of the entry 
echo "revenue: ".$row['sum']; 
+0

辉煌的伴侣谢谢! – pjmil

0

如果SID销售和服务都是相同的,独一无二的每一个销售,那么你可以使用这样的查询:

$result = mysql_query(SELECT Sale.saleID, Sale.assetID, Sale.eID, Sale.sID, Sale.pID, Sale.saDate, Service.revenue "."FROM Sale, Service "."WHERE Sale.sID = Service.sID AND Sale.saDate = CURDATE()) or die(mysql_error()); 

或者,如果Service.sID = Sale.saleID然后更改acordingly。

通过这种方式,您可以控制您想要从每个表中检索哪个列。

+0

sID! = saleID,服务更多的是销售内容(例如移动40美元计划)以及产品(例如iPhone)和销售员工(例如Tom)的个人资料。我想在这里做的是显示当天所有销售的总收入。就像实时显示企业的收入一样。这是否更有意义? – pjmil

+0

如果sId!= saleID,那么我会推荐在Service表中包含saleID,并将where子句更改为:WHERE Sale.saleID = Service.saleID AND Sale.saDate = CURDATE(),我希望这可以解决您的问题。 –