Here, I am creating an alias for the inner selection, but it automatically adds the table prefix to the alias table. My sql query is as follows:
SELECT
`tb1`.*, `tb2`.*, `tb3`.`client_name`,
`cms_tb4`.*, `tb1`.`project_status` AS pstat
FROM
(`cms_projects` tb1)
JOIN `cms_project_type` tb2 ON `tb2`.`id` = `tb1`.`project_type`
JOIN `cms_clients` tb3 ON `tb1`.`client_id` = `tb3`.`client_id`
JOIN (
SELECT
*
FROM
(
SELECT
t1.project_id AS projid,
t2.work_id,
t2.work_date
FROM
cms_projects_mod t1,
cms_work_status_emp t2
WHERE
t1.module_id = t2.pro_module_id
ORDER BY
t2.work_date DESC
) AS subtb4
GROUP BY
projid
ORDER BY
work_date DESC
) AS tb4 ON `cms_tb4`.`projid` = `tb1`.`project_id`
LIMIT 50
i am working on codeigniter framework.
in codeigniter model just like this
$this->db->select('tb1.*');
$this->db->select('tb1.project_status as pstat');
$this->db->from('cms_projects tb1');
$this->db->join('project_type tb2', 'tb2.id=tb1.project_type');
$this->db->join('clients tb3', 'tb1.client_id = tb3.client_id');
$this->db->join("(
select * from (
select t1.project_id as projid, t2.work_id,t2.work_date from cms_projects_mod t1, cms_work_status_emp t2 where t1.module_id = t2.pro_module_id order by t2.work_date desc) as subtb4
group by projid order by work_date desc ) as tb4 ",
"tb4.projid = tb1.project_id" ,false);
$this->db->group_by("tb1.project_id");
$this->db->order_by("tb3.work_date",'desc');
here i didn't give any prefix in third join tb4 but CI Add automatically when the running time.
Please check the code order in CodeIgniter Documentation.
$this->db
->select()
->from()
->join()
This is the correct order. Changing the order sometimes causes issues in complex queries.
Visit this link https://github.com/bcit-ci/CodeIgniter/issues/2975
Why you using query strings? If you using coeignoter why dont you use active record. Active Record queries are automatically protected with sql injection.
you can do kind of this....its not same you want just idea how you can use active record sql.
$query = $this->db->select('*',FALSE)
->from('tb1','tb2'.....)
->join('tb2', 'tb1.cms_project_type = tb2.cms_project_type','left')
->join('tb3', 'tb1.cms_project_type = tb2.cms_project_type','left')
->group_by('projid')
->order_by('work_date','DESC')
->get();
https://ellislab.com/codeigniter/user-guide/database/queries.html
Hope this help.
Related
I want to use mysql's "JOIN"
I want to group rows by "date_text" where "tokenIdx" is "1001" and "datetime_unix" is the highest value.
Is my code wrong?
SELECT `A.idx`
FROM `data_candle_h1` 'A'
JOIN
(
SELECT `date_text`, MAX(`datetime_unix`) AS 'datetime_unix'
FROM `data_candle_h1`
WHERE `tokenIdx` = '1002'
GROUP BY `date_text`
) 'B'
ON `A.datetime_unix` = `B.datetime_unix`
WHERE `A.tokenIdx` = '1002'
Your query is syntactically perfect. Just remove single quotes('') around table aliases (A and B). I have corrected it. Please check this out.
SELECT `A.idx`
FROM `data_candle_h1` A
JOIN
(
SELECT `date_text`, MAX(`datetime_unix`) AS 'datetime_unix'
FROM `data_candle_h1`
WHERE `tokenIdx` = '1002'
GROUP BY `date_text`
) B
ON `A.datetime_unix` = `B.datetime_unix`
WHERE `A.tokenIdx` = '1002'
I have a MySQL query like bellow, and I want to use it in laravel controller,
I know how to use join in laravel, but I don't know how to write it when we have another sub query in the join statement,
SELECT barangs.id, barangs.kode_barang, barangs.nama_barang,
IFNULL(a.QTY_IN,0) AS masuk, IFNULL(b.QTY_OUT,0) AS keluar,
IFNULL(c.PO_RETUR_QTY,0) AS po_retur, IFNULL(d.CUS_RETUR_QTY,0) AS cus_retur,
barangs.stok AS stok_akhir, barangs.harga_beli
FROM barangs
LEFT JOIN (
SELECT barang_masuk_detail.id_barang, barang_masuk.tgl_masuk, SUM(barang_masuk_detail.qty_terima) AS QTY_IN
FROM barang_masuk
LEFT JOIN barang_masuk_detail ON barang_masuk.id = barang_masuk_detail.id_brg_masuk
/* WHERE barang_masuk.tgl_masuk BETWEEN "2015-08-01" AND "2015-08-31" */
GROUP BY barang_masuk_detail.id_barang ASC
)AS a ON a.id_barang = barangs.id
LEFT JOIN (
SELECT barang_keluar_detail.id_barang, barang_keluar.tgl_kirim, SUM(barang_keluar_detail.qty_dikirim) AS QTY_OUT
FROM barang_keluar
LEFT JOIN barang_keluar_detail ON barang_keluar.id = barang_keluar_detail.id_brg_keluar
/* WHERE barang_keluar.tgl_kirim BETWEEN "2015-08-01" AND "2015-08-31"*/
GROUP BY barang_keluar_detail.id_barang ASC
)AS b ON b.id_barang = barangs.id
/*join retur try */
LEFT JOIN (
SELECT retur_beli_detail.barang_id, retur_beli.tgl_retur, SUM(retur_beli_detail.retur_qty) AS PO_RETUR_QTY
FROM retur_beli
LEFT JOIN retur_beli_detail ON retur_beli.id = retur_beli_detail.retur_id
/* WHERE retur_beli.tgl_retur BETWEEN "2015-08-01" AND "2015-08-31"*/
GROUP BY retur_beli_detail.barang_id ASC
)AS c ON c.barang_id = barangs.id
/*join retur dari customer */
LEFT JOIN (
SELECT retur_kirim_detail.barang_id, retur_kirim.tgl_retur, SUM(retur_kirim_detail.retur_qty) AS CUS_RETUR_QTY
FROM retur_kirim
LEFT JOIN retur_kirim_detail ON retur_kirim.id = retur_kirim_detail.retur_id
/* WHERE retur_kirim.tgl_retur BETWEEN "2015-08-01" AND "2015-08-31"*/
GROUP BY retur_kirim_detail.barang_id ASC
)AS d ON d.barang_id = barangs.id
if I run in the sqlyog it works as expected, but not sure how to write it in laravel syntax,
oh if possible to filter it using whereBetween in laravel base on request send from user, where I need put the syntax ?
like whereBetween([ $request->startDate, $request->endDate ]);
For the simple way to execute query with below syntax:-
DB::select("select * from table JOIN ...");
This will provide you collection of filtered data.
Edited Code
to use whereBetween() Laravel where query you need to write MySQL query like below
$startDate = $request->startDate;
$endDate = $request->endDate;
DB::select("
select * from table JOIN ...
where fieldName BETWEEN '$startDate' AND '$endDate'
");
To add more conditions, we need to write Plain MySQL query.
Hi, I need a zf2 join query that fetches only the latest row(by id DESC) from the second table. I have written an sql query and it works.
SELECT st1.customer_id,
st1.id
FROM status st1
inner JOIN
(
SELECT max(id) MaxId, customer_id
FROM status
GROUP BY customer_id
) st2
ON st1.customer_id = st2.customer_id
AND st1.id = st2.MaxId
But I need this query at zend framework 2 table gateway format. Please help.
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Expression;
$sql = new Select ();
$sql->columns(["customer_id", new Expression ("max(id) AS MaxId")])
->from ('status')
->group('customer_id');
$outer = new Select ();
$outer->columns (['customer_id', 'id'])
->from (['st1' => 'status'])
->join (['st2' => $sql],
'st1.customer_id = st2.customer_id AND st1.id = st2.MaxId', []);
I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan
Here are the relevant columns of my tables
bia_panels ( id, sign_id, value, project_id )
bia_clients ( id, name )
bia_projects ( id, name, client_id, city_id )
bia_cities ( id, name )
I am attempting to update the bia_panels.project_id to the bia_projects.id where the bia_panels.value = bia_clients.name and the panels.project_id =000 and the value is not blank of course I must use multiple joins to get there
-- UPDATE
SELECT * FROM
`bia_panels` AS t1
JOIN bia_clients AS t2
ON t1.value = t2.name
JOIN bia_projects AS t3
ON t2.id = t3.client_id
-- SET t1.project_id = t3.id
-- WHERE t1.value<>'' AND t1.project_id = '000'
WHERE t1.value <>''
The problem is that this is not giving me the correct results (my project ids are not correct somewhere in the joins multiple results are returned so they break
I know that once I am able to get the select portion correct I can use an update
For example there may be multiple panels where the value=client.name but not all of them are the same project ID
and bia_panels.ID = bia_panels.project_id
join condition is missing in your select query, this should be added like
JOIN bia_projects ON bia_clients.id = bia_projects.client_id and bia_panels.ID = bia_panels.project_id
following query should give right output
SELECT sign_id, value, left(sign_id, 3) AS city_ID ,
bia_clients.id AS 'client id', bia_projects.id AS proj_id , bia_cities.id
FROM bia_panels
JOIN bia_clients ON bia_panels.value = bia_clients.name
JOIN bia_projects ON bia_clients.id = bia_projects.client_id and bia_panels.ID = bia_panels.project_id
JOIN bia_cities ON bia_projects.city_id = bia_cities.id WHERE bia_panels.value<>'' AND bia_panels.project_id = '000' ORDER BY value
I would little bit reorganize your query into UPDATE query:
UPDATE bia_panels p, bia_clients c, bia_projects t
SET p.project_id=t.id
WHERE p.value=c.name
AND t.client_id=c.id
AND p.project_id=''