How to Translate Codeigniter Pre Formatted Query Into an Original Mysql Query (e.g $this->db->select('table1.*,table2.*,table3.*'))

…衆ロ難τιáo~ 提交于 2020-06-29 04:19:05

问题


I have a website which is using a CodeIgniter framework and almost using CodeIgniter's preformatted query class in every request to process data from database built by my ex coworker.

One of the example is like this:

function tampilkan_data(){
      $this->db->select('barang.*,barang_keluar.*,barang_masuk.*');
      $this->db->from('barang');
      $this->db->join('barang_keluar', 'barang.id_barang=barang_keluar.id_barang','left');
      $this->db->join('barang_masuk', 'barang.id_barang=barang_masuk.id_barang','left');
      $this->db->order_by('barang.id_barang','asc');
      $this->db->order_by('barang.kode_barang','asc');
      $this->db->where('barang.status','0');
      return $query = $this->db->get()->result();
  }

I want to change the result from that query, but in order to understand it first I have to translate it into an original MySQL query. Attempted to do this:

SELECT `barang`.*, `barang_keluar`.*, `barang_masuk`.*
FROM `barang` AS A
LEFT JOIN `barang_keluar` AS B ON A.id_barang = B.id_barang
LEFT JOIN `barang_masuk` AS C ON A.id_barang = C.id_barang
ORDER BY A.id_barang ASC, A.kode_barang ASC
WHERE A.status = 0

But I got this error

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE A.status = 0 LIMIT 0, 25' at line 6

I tried to remove the WHERE A.status = 0 to see which is wrong from my code the I got this error

1051 - Unknown table 'barang'

I thought the problem is in the line SELECT barang.*, barang_keluar.*, barang_masuk.*, because I didn't really know how to write it in an MySQL original formatted.

I already searched for this topic and I even tried to write the query straight to the search bar but I didn't find the right approach according to my case. Hope someone can help


回答1:


You can simply print your query, Then you can see your query in original sql format.

function tampilkan_data(){
    $this->db->select('barang.*,barang_keluar.*,barang_masuk.*');
    $this->db->from('barang');
    $this->db->join('barang_keluar', 'barang.id_barang=barang_keluar.id_barang','left');
    $this->db->join('barang_masuk', 'barang.id_barang=barang_masuk.id_barang','left');
    $this->db->order_by('barang.id_barang','asc');
    $this->db->order_by('barang.kode_barang','asc');
    $this->db->where('barang.status','0');
    $query = $this->db->get()->result(); // notice that i have removed the return keyword
    echo $this->db->last_query(); // This will print your query directly on the screen.
}

But the error which you are getting is -> You don't have a table named barang. Make sure that the table exists.

Hope It Helps...




回答2:


Your query is almost ok. But you will have to change it on order by. Because u have used asc more than once, that is not supported by mysql as well. You need to modify your query like below

    SELECT A.*, B.*, C.*
FROM barang AS A
LEFT JOIN barang_keluar AS B ON A.id_barang = B.id_barang
LEFT JOIN barang_masuk AS C ON A.id_barang = C.id_barang 
WHERE A.status = 0 ORDER BY A.id_barang, A.kode_barang ASC

Now be confirm that you have table barang in your database and try again. If you face another sql problem just reply with your screenshot of code snippet here.




回答3:


If you want to look at the query string generated by the CI Query builder then you can log this method $this->db->last_query() which will give the last SQL query as string executed by it.



来源:https://stackoverflow.com/questions/57702792/how-to-translate-codeigniter-pre-formatted-query-into-an-original-mysql-query-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!