CI高级SQL查询?

时间:2015-11-02 04:18:49

标签: php mysql codeigniter

感谢您帮我解决这个问题:

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

现在,因为我是框架的新手。任何人都可以帮助我将此查询转换为CodeIgniter 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

对于附加问题,在何处放置或如何使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';

1 个答案:

答案 0 :(得分: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

如果你想将查询转换为codeigniter,它应该是这样的,

$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()