Convert Join to sub Query - eloquent

I have a join and i want to convert it to subQuery. Can someone please help me to do the same.
Here is the join with db table i have:
$jobId = 12;
$customerId = 15;
DB::table('user_access_customer_job')
->join('user_access_customer_job as parent_jobs', 'user_access_customer_job.parent_job_id', '=', 'parent_jobs.job_id')
->where('parent_jobs.job_id', '<>', $jobId)
->where('parent_jobs.customer_id', $customerId)
->where('user_access_customer_job.job_id', '<>', $jobId)
->whereRaw('parent_jobs.user_id <=> user_access_customer_job.user_id')
->whereRaw('parent_jobs.type = user_access_customer_job.type')
->select('parent_jobs.id')
->lists('id');

Related

How to select all values with latest dates Laravel 5.8

I am trying to get all of the rows with the latest dates
I want to get the rows 3 and 4 only
When I use this eloquent query:
$items = Purchase::whereYear('created_at', '=', $year)
->whereMonth('created_at', '=', $month)
->where('created_at', Purchase::max('created_at'))
->get();
return $items;
Yet this is what I get:
[{"id":4,"itemid":1,"price":"1.60","quantity":4,"created_at":"2019-09-14 08:39:31","updated_at":"2019-09-14 08:39:31"}]
You must be use ->whereDate() function
$lastDate = Purchase::max('created_at')->format('Y-m-d');
$items = Purchase::whereDate('created_at', $lastDate)->get();
Edit:
$lastDate = Carbon::parse(Purchase::max('created_at'))->format('Y-m-d');
$items = Purchase::whereDate('created_at', $lastDate)->get();

change query from postgresql into oracle

how change this query from postgresql into oracle?
SELECT tbmm.tbmm_id,
tbmm.tbmm_email AS seller_mail,
'FB' AS seller_type,
concat(tbmm.tbmm_firstname, COALESCE(tbmm.tbmm_middlename),
COALESCE(tbmm.tbmm_lastname)) AS seller_owner_name,
tbmss.tbmss_name AS seller_store_name,
tbmss.tbmss_description AS seller_description,
tbmss.tbmss_phone AS seller_phone,
btrim(concat(COALESCE(tbmma.tbmma_addr1), COALESCE(tbmma.tbmma_addr2,
tbmma.tbmma_addr1), COALESCE(tbmma.tbmma_addr3, tbmma.tbmma_addr1),
COALESCE(tbmkc.tbmkct_nama), COALESCE(tbmkl.tbmkl_nama),
COALESCE(tbmma.tbmma_postcode, '0') AS seller_store_address,
tbmb.tbmb_name AS bank_name,
concat(tbmb.tbmb_name, COALESCE(tbmsd.tbmsd_bank_account_branch) AS
bank_desc,
tbmsd.tbmsd_bank_account_no AS bank_account_no,
tbmsd.tbmsd_bank_account_name AS bank_account_name,
tbmsd.tbmsd_no_id AS seller_id_card,
'F' AS flag_alfamind,
0 AS commision,
tbmm.tbmm_mage_entity_id,
tbmm.tbmm_update_date AS member_update_date,
tbmss.tbmss_update_date AS store_update_date,
tbmsd.tbmsd_update_date AS document_update_date,
tbmma.tbmma_update_date AS address_update_date
FROM coreapp.tb_master_member tbmm
JOIN coreapp.tb_master_seller_store tbmss ON tbmm.tbmm_id =
tbmss.tbmm_id
JOIN coreapp.tb_master_seller_document tbmsd ON tbmsd.tbmm_id =
tbmm.tbmm_id
JOIN coreapp.tb_master_bank tbmb ON tbmb.tbmb_id = tbmsd.tbmb_id
JOIN coreapp.tb_master_member_group tbmg ON tbmg.tbmm_id = tbmm.tbmm_id
LEFT JOIN coreapp.tb_master_member_address tbmma ON tbmma.tbmm_id =
tbmm.tbmm_id AND tbmma.tbmma_address_type = 140 AND tbmma.tbmma_status = 23
LEFT JOIN coreapp.tb_master_kecamatan tbmkc ON
tbmkc.tbmkct_kecamatan_id = tbmma.tbmkct_kecamatan_id
LEFT JOIN coreapp.tb_master_kelurahan tbmkl ON tbmkl.tbmkl_kelurahan_id
= tbmma.tbmkl_kelurahan_id
WHERE tbmg.tbmg_id = 4;
i have try to query on my sql developer but something error with query
now I'm just try until one day and still can't run

Zend 2: How do I execute multiple SQL queries using Zend\Db\Adapter?

Im executing this query:
$query = "
Select * From table1 Where id = 1;
Select * From table2 Where name = 'test';
";
With \Zend\Db\Adapter\Adapter:
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();
How can I access the second result?
$rawResult->next() only returns values from first query.
Use two different queries.
/*
* First Query
*/
$query = "
Select * From table1 Where id = 1
";
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();
/*
* Second Query
*/
$query = "
Select * From table2 Where name = 'test'
";
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();
I don't believe it works the way you had it to where two queries in a row are sent this way.
To avoid code duplication you can wrap the duplicated code into a method.
$rawResult1 = $this->getResults("Select * From table1 Where id = 1");
$rawResult2 = $this->getResults("Select * From table2 Where name = 'test'");
function getResults(string $query)
{
$stmt = $this->dbAdapter->query($query);
return $stmt->execute();
}

Pig Order By Query

grunt> dump jn;
(k1,k4,10)
(k1,k5,15)
(k2,k4,9)
(k3,k4,16)
grunt> jn = group jn by $1;
grunt> dump jn;
(k4,{(k1,k4,10),(k2,k4,9),(k3,k4,16)})
(k5,{(k1,k5,15)})
Now, from here I want the following output :
(k4,{(k3,k4,16),(k1,k4,10)})
(k5,{(k1,k5,15)})
Bascially, I want to sort on the numbers : 10,9,16 and select the top 2 for every row.
How do I do it?
This is similar to this question and you could use a Nested FOREACH, e.g.:
A = LOAD 'data';
jn = group A by $1;
B = FOREACH jn {
sorted = ORDER A by $2 ASC;
lim = LIMIT sorted 2;
GENERATE lim;
};
DUMP B;

Zend_DB_Select : why are all fields returned?

public function getWorksheetData($id) {
/** create the following query using select object:
SELECT wc.label, wd.notes FROM worksheet_data wd
LEFT JOIN worksheet_columns wc ON wd.column_id = wc.id;
*/
$id = (int) $id;
$select = $this->_db->select()
->from(array('wd'=>'worksheet_data'),
array('wc.label','wd.notes'))
->join(array('wc'=>'worksheet_columns','wd.column_id = wc.id'))
->where("wd.id = :worksheet_id");
$results = $this->_db->fetchAll($select, array('worksheet_id' => $id),Zend_Db::FETCH_ASSOC);
return array('results'=>$results);
}
Why does this query become:
SELECT wc.label, wd.notes, wc.* FROM worksheet_data AS wd INNER JOIN worksheet_columns AS wc WHERE (wd.id = :worksheet_id)
and return wc.*?
You need to put an empty array as the third argument to the join method, also the join condition should not be part of the first argument's array, but as the second argument instead (you'll notice that there is not join condition in your query). Join needs at least the first two arguments, this is the method signature:
join(table, join, [columns])
Applying that to your join method:
->join(array('wc'=>'worksheet_columns'),'wd.column_id = wc.id', array())
so:
$select = $this->_db->select()
->from(array('wd'=>'worksheet_data'),
array('wc.label','wd.notes'))
->join(array('wc'=>'worksheet_columns'),'wd.column_id = wc.id', array())
->where("wd.id = :worksheet_id");
gives the output:
SELECT `wc`.`label`, `wd`.`notes` FROM `worksheet_data` AS `wd` INNER JOIN `worksheet_columns` AS `wc` ON wd.column_id = wc.id WHERE (wd.id = :worksheet_id)
The manual says:
To select no columns from a table, use
an empty array for the list of
columns.