2012-09-09 45 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(),我希望這可以解決您的問題。 –