2015-11-02 75 views
1

感謝幫助我在這個問題上:CI高級SQL查詢?

http://stackoverflow.com/questions/33182460/mysql-data-collection-multiple-table 

而現在,因爲我是在框架的新手。誰能幫我這個查詢轉換成能由笨3理解和正確運行:

select sales.id, sales.refid, reference.refname, sales.invoice, 
sales.itemid, sales.price as sellprice, purchase.price as buyprice, 
sales.price - purchase.price as profit from sales 
left outer join reference on reference.refid = sales.refid 
left outer join purchase on purchase.itemid = sales.itemid 

而對於addtional問題在什麼地方或如何使CI執行多個查詢,例如:

INSERT INTO sales VALUES (null , 001 , 1501 , ss01 , 12000); 
INSERT INTO purchase VALUES (null , ss01 , 10200); 
UPDATE reference SET purchasedate = now() WHERE refid = 'ss01'; 

回答

0

我認爲不需要將查詢轉換爲codeigniter。

只需使用query()。在CI中。

$query = "select sales.id, sales.refid, reference.refname, sales.invoice, 
sales.itemid, sales.price as sellprice, purchase.price as buyprice, 
sales.price - purchase.price as profit from sales 
left outer join reference on reference.refid = sales.refid 
left outer join purchase on purchase.itemid = sales.itemid"; 

$this->db->query($query 

如果你想查詢到笨轉換,它應該是這樣的,

$this->db->select("select sales.id, sales.refid, reference.refname, sales.invoice,sales.itemid, sales.price as sellprice, purchase.price as buyprice,sales.price - purchase.price as profit"); 
$this->db->join("reference", "reference.refid = sales.refid", "left outer"); 
$this->db->join("purchase", "purchase.itemid = sales.itemid", "left outer"); 
$this->db->get("sales"); 

第二部分

用於該用途的交易,

$this->db->trans_start(); 
$this->db->query('AN SQL QUERY...'); 
$this->db->query('ANOTHER QUERY...'); 
$this->db->query('AND YET ANOTHER QUERY...'); 
$this->db->trans_complete(); 

詳情請點擊此處refernce

或者使用mysql_multi_query()

mysql_multi_query()

+0

看到這一點,怎麼樣第二個問題?關於多個查詢? –

+0

@SuryaTanamas答案已更新。 –