How we can write multiple select keyword in a single mysql query using Magento? - magento

here is my query and this give proper ouput.
But How can I set it according to magento rules?
SELECT main_table.*,
(select company from sales_flat_order_address sfoa where sfoa.entity_id=sfo.billing_address_id) as bill_to_company,
(select company from sales_flat_order_address sfoa where sfoa.entity_id=sfo.shipping_address_id) as ship_to_company
FROM sales_flat_order_grid as main_table inner join sales_flat_order as sfo
on main_table.entity_id = sfo.entity_id
please help me....
Thanks & Reagards
Praful

You can use magento default connection Model
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$result=$write->query("your query as like mysql query no different");
$row = $readresult->fetch(); // this will fetch all your data

Related

Crudbooster laravel - How to make different column relationship?

I have two table 1. table_a 2. table_b
And all table have company_id. I want to make relationship among this two table using same company id . Like
select * from table_a left join table_b on table_a.company_id = table_b.company_id;
Please help me out if its really possible. Thank you in advance.
You can use this:
$data = DB::table('table_a')->leftJoin('table_b', 'table_a.company_id', '=', 'table_b.company_id')->get();
Or raw:
$users = DB::table('table_a')
->select(DB::raw('query here'))
->get();
More on:
https://laravel.com/docs/5.7/queries

Laravel 5.4 Eloquent multiple table query

Hi I want to form a query in laravel 5.4 eloquent for the following SQL:
select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id
All the foreign keys are properly given and the models are also properly created.
My aim is to get all the master record name like category, client name, etc to come in the child table record set directly.
You can try this:
DB::table('payments')
->select('payments.*',DB::raw('CONCAT(clients.firstname," ",clients.lastname) as clientname'),categories.category)
->join('clients','clients.id','=','payments.client_id')
->join('categories','categories.id','=','payments.category_id')
->get();
Hope this help for you !!!
Paste your row query in DB::row() and print data like following
$data= DB::row("select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id")->get();
dd($data);
if your query is right and properly managed relation than its retrieve data
If you already made Models than Why you don't use ORM?
Use Models,
payment::leftJoin('clients ','payments.client_id','=','clients.id')
->leftJoin('categories ','payments.category_id ','=','categories .id')->select(['payment.*','categories.category',DB::raw("CONCAT(clients.firstname,' ',clients.lastname) as clientname")])->get();

Rewrite SQL Query for Use With Laravel Query Builder

$query = "SELECT *, (id.number_of_candidates - t.counted) as available
FROM interview_dates id
LEFT JOIN (SELECT interview_dates_id,COUNT(interview_dates_id) as counted
FROM date_timeslot GROUP BY interview_dates_id) as t
ON t.interview_dates_id=id.id
WHERE id.number_of_candidates > t.counted";
Please help me to rewrite this using Laravel Query Builder.

laravel about inner join and subquery

ok, now ,I want get sql just like:
select field1,field2,field3 from orders
inner join
(select id from orders where field4=3 limit 1000, 20)
as temp using(id)
how can I get this by laravel 5.1?
er, sorry I poor in english. What I mean is I want get the native sql like that , and now I don't know what can I do with laravel DB or ORM. I create a model Order corresponding to the table orders.
thank you ~
Although it is not a good way to do it because laravel supports inner joins in query builder. You can do it like this:
DB::select(DB::raw(" select field1,field2,field3 from orders
inner join
(select id from orders where field4=3 limit 1000, 20)
as temp using(id)
"))->get();
Use the following code
DB::select(DB::raw(" select field1,field2,field3 from orders
inner join
(select id from orders where field4=3 limit 1000, 20)
as temp using(id)
"))->get();

magento sql query update category from inactive to is active

I currently have an magento site with a couple of hundred categories, the problem I have is the person who added them set is active to NO. I would like to update all the main and sub categories to is_active to be YES (TRUE).
I have tried the following update Query which updated the is_active to 1 but does not update in Magento even after I re indexed all in Index Management.
UPDATE catalog_category_flat_store_1 SET is_active = 1 WHERE is_active = 0
Thanks for your help.
The catalog_category_flat_store_1 table is a generated table from the EAV tables done for performance reasons. It's not the true source of the category config data.
You'll need to update the data in the EAV. Run this query to find where is_active is stored:
SELECT t1.attribute_id, t1.attribute_code, t1.backend_type
FROM eav_entity_type AS t0
INNER JOIN eav_attribute AS t1 ON (t0.entity_type_id = t1.entity_type_id)
WHERE (t0.entity_model = 'catalog/category')
On my server it has an attribute_id of 42.
You'll then need to query catalog_category_entity and join on catalog_category_entity_int to get the rows to update.

Resources