MySQL Left/Right/Outer Join - mysql

I want to display supplier_address of active user only. Below is the database of the system.
Illustration of my db
Like I mentioned above, I want to display supplier_address of active suppliers only. The image provided above, shows A - supplier_address table and B - status_login table which hold the "active" element.
Right now, my coding look like this,
$activeSupplier = "Active";
// fetch data for 'ACTIVE' Supplier Basic Info Table from table (supplier_profile)
$supplierAddressInfoListTable = SupplierAddress::find()
->select(['supplier_address.supplier_addressID AS supplierAddressID', `
'supplier_address.supplier_profile_ID AS supplierID', 'supplier_address.address_one AS supplierAddressone',`
'supplier_address.address_two AS supplierAddresstwo', 'supplier_address.postcode AS supplierPostcode',
'supplier_address.city AS supplierCity', 'status_login.description AS supplierLoginstatus',
'state.stateID AS supplierStateID', 'state.name AS supplierStatename',
'country.countryID AS supplierCountryID', 'country.name AS supplierCountryname'])
->leftJoin('supplier_profile', 'supplier_profile.supplier_profileID = supplier_address.supplier_profile_ID')
->leftJoin('state', 'state.stateID = supplier_address.state_ID')
->leftJoin('country', 'country.countryID = supplier_address.country_ID')
->where(['=', 'status_login.description', $activeSupplier])
->orderBy(['supplier_profile.supplier_profileID' => SORT_ASC])
->asArray()
->all();
I don't know how I can get by without having to rely on a temporary table. If you guys have some suggestion, please do recommend here.

Also you need to join the login_profile and status_login tables.
`
$activeSupplier = "Active";
// fetch data for 'ACTIVE' Supplier Basic Info Table from table (supplier_profile)
$supplierAddressInfoListTable = SupplierAddress::find()
->select(['supplier_address.supplier_addressID AS supplierAddressID', `
'supplier_address.supplier_profile_ID AS supplierID', 'supplier_address.address_one AS supplierAddressone',`
'supplier_address.address_two AS supplierAddresstwo', 'supplier_address.postcode AS supplierPostcode',
'supplier_address.city AS supplierCity', 'status_login.description AS supplierLoginstatus',
'state.stateID AS supplierStateID', 'state.name AS supplierStatename',
'country.countryID AS supplierCountryID', 'country.name AS supplierCountryname'])
->leftJoin('supplier_profile', 'supplier_profile.supplier_profileID =
supplier_address.supplier_profile_ID')
->leftJoin('login_profile', 'supplier_profile.supplier_profileID =
login_profile.supplier_profile_ID')
->leftJoin('status_login', 'login_profile.status_login_ID =
status_login.status_login_ID')
->leftJoin('state', 'state.stateID = supplier_address.state_ID')
->leftJoin('country', 'country.countryID = supplier_address.country_ID')
->where(['=', 'status_login.description', $activeSupplier])
->orderBy(['supplier_profile.supplier_profileID' => SORT_ASC])
->asArray()
->all()
`

Related

importing data from another database based on field value

I have a website where I am trying to setup a page where someone would have to enter a number into a field, which would correspond to a field in the other database, and import (add) the records from that database, into a table of another database, and I am having trouble with that.
I know how to update and add data from table to table within the same database, but just unsure how I would do that from another database.
This is the code that I would use to add data to another table
$data = array();
$keyvalues = array();
$data["Number"] = $values['Number'];
$data["Rider Name"] = $values['Rider Name'];
$data["Horse Name"] = $values['Horse Name'];
$data["Division Name"] = $values['Division Name'];
$data["Level"] = $values['Level'];
$data["Division Type"] = $values['Division Type'];
$data["Show Type"] = $values['Show Type'];
$data["Dressage Test"] = $values['Dressage Test'];
$keyvalues["Number"] = $values['Number'];
if ($values['Number'] = $values['Number']){
DB::Insert("Scoring", $data, $keyvalues );
}
But not sure how I would do it from another database. Any help would be appreciated.

How to save data from a table from another database in Laravel?

I need to save data from an order table from one database to another through Laravel. I created a function in my controller like this:
public function getMarketplace()
{
$orderoc = OrderOC::orderBy('oc_order.date_added', 'desc')
->join('oc_order_product', 'oc_order_product.order_id', 'oc_order.order_id')
->join('oc_order_history', 'oc_order_history.order_id', 'oc_order.order_id')
->where('oc_order_history.order_status_id', '=', '17')
->get();
foreach($orderoc as $oc){
$ordererp = new Order;
$ordererp->erp_createdid = $oc->created_id;
$ordererp->erp_marketplaceid = 1;
$ordererp->erp_site = rand(1,100000000);
$ordererp->erp_payment_method = $oc->payment_method;
$ordererp->erp_orderdate = $oc->date_added;
$ordererp->erp_orderaprove = $oc->date_added;
$ordererp->erp_billingid = 1;
$ordererp->erp_shippingid = 1;
$ordererp->erp_marketplace = 'Comércio Urbano';
$ordererp->erp_orderquantity = $oc->quantity;
$ordererp->erp_erro = '';
$ordererp->erp_product_ok = 1;
$ordererp->erp_compraId = null;
$ordererp->save();
if(strlen($oc->created_id) == 0){
$oc->created_id = rand(1,10000000);
$oc->save();
}
$orderprod = new OrderProduct;
$orderprod->erp_productid = $oc->product_id;
$orderprod->erp_createdid = $oc->created_id;
$orderprod->erp_model = $oc->model;
$orderprod->erp_quantity = $oc->quantity;
}
}
One table is from my ERP and the other is responsible for receiving OpenCart purchases, but every time I run, the same product appears more than once in my order table.
(It is possible to see through the purchase date, since created_id is created in the controller function)
Does anyone know how to tell me why data is duplicated when inserted inside a foreach? This is not the first time, if you tell me a more robust way of doing the job, I'm grateful. Any suggestion? Thank you in advance!
One possiblity is you put unique validation on a table that receive the data

insert data in multiple table with one function in laravel

I'm trying to add values in multiple tables with the same function but I get an error that the id and product_id can't be null !! even though they are set. Here's my code:
$parentproduct=new Product();
$parentproduct->id=Input::get('id');
$insertedId = $parentproduct->id;
$parentproduct->save();
$product=new ProductsTranslation();
$product->id=Input::get('id');
$product->product_id =Input::get('insertedId');
$product->title=Input::get('title');
$product->content=Input::get('content');
$product->price=Input::get('price');
$product->description_title=Input::get('description_title');
$product->prod_info_title=Input::get('prod_info_title');
$product->prod_info=Input::get('prod_info');
$product->save();
Looks like you need to move a few things around here...
This $insertedId = $parentproduct->id; wont return a value until you've ran `->save().
Also, your second statement is trying to get an Input::('insertedId') but you're setting a variable above.
Try this:
$parentproduct = new Product();
$parentproduct->id = Input::get('id');
$parentproduct->save();
$insertedId = $parentproduct->id;
$product = new ProductsTranslation();
$product->id = Input::get('id');
$product->product_id = $insertedId;
$product->title = Input::get('title');
$product->content = Input::get('content');
$product->price = Input::get('price');
$product->description_title = Input::get('description_title');
$product->prod_info_title = Input::get('prod_info_title');
$product->prod_info = Input::get('prod_info');
$product->save();

Pull SKU, Manufacturer and custom attribute from Magento database

I'm trying to pull a full list of products from the database with the SKU, manufacturer and a custom attribute called 'GTIN'.
I'm really struggling with the custom attribute part.
This statement works to pull the manufacturer and SKU
SELECT cpe.sku, m1.manufacturer,
FROM catalog_product_entity AS cpe
INNER JOIN exportview_manufacturer AS m1 ON cpe.entity_id = m1.entity_id
My MySQL is very poor and I can't seem to get this custom attribute. I've found the following statement online which I KNOW has all the details I need to make this work but thus far I've been getting my head in a mess trying to implement it
SELECT e.entity_id AS product_id, var.value AS product_name
FROM catalog_product_entity e, eav_attribute eav, catalog_product_entity_varchar var
WHERE
e.entity_type_id = eav.entity_type_id
AND eav.attribute_code = 'gtin'
AND eav.attribute_id = var.attribute_id
AND var.entity_id = e.entity_id
I simply need the first statement to also include the 'gtin' column, but joining is where I'm falling short. Can someone please assist?
You need to create one PHP script to get all product and it's attribute. So first create all_product.php file on your Magento root and add below code.
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../app/Mage.php';
Mage::app();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
foreach ($collection as $product) {
echo $product->getName(); //get name
/*you can get any attribue you want like name above. Even you can get all custom attribule for product for that you need to use print_r($collection->getData()) above foreach*/
}
Hope it will work.

Laravel: Querying based on Input. If input is empty, get all

I have a calendar (FullCalendar) where the user can filter down results based on a few params (Tutor Secondary Tutor, Lesson, Location). When the user makes a change to the query it hits the following code.
The issue I am having is the 'OR'. What I really want is an IF input is null then get all.
If User { Get all lessons where lead_tutor_id = 1 and secondary_tutors_id = 1 }
If User and Location { Get lessons where the user is as above, but have location_id = 3 }
etc, etc.
So, is there a way I can fall back to get ALL the results IF only one or two filters are set?
$current_events = Calendar::Where(function($query) use ($start_time, $end_time, $tutor, $location, $lesson)
{
$query->whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')
->whereRaw('lead_tutor_id = ?
OR secondary_tutors_id = ?
OR location_id = ?
OR lesson_id = ?',
[
$tutor, // Input get() for user
$tutor, // Input get() for user
$location, // Input get() for location
$lesson, // Input get() for lesson
]
);
})->with('lessons', 'leadtutor', 'secondarytutor')->get();
I've been playing with Query Scopes, but this seems to fail if passing a NULL value through to it.
Any help is very much appreciated. Thanks in advance.
You can build the query on forehand, store it in a variable and use it once its build.
$query = isset($var) ? $var : '';
$query .= isset($othervar) ? $othervar : '';
whereBetween(*)->orderBy(*)->whereRaw($query)
Only thing you need to keep in mind is to insert the 'OR's in the right place . So have like a check for wether it is the first thing to be inserted or not, if not then put 'OR' in front of it.
Hope that is enough info to help you.
After the advice from Saint Genius, I have got this working:
$built_query = [];
isset($lead_tutor) ? $built_query['lead_tutor'] = 'lead_tutor_id = ' . $lead_tutor . ' ' : null;
isset($secondary_tutor) ? $built_query['secondary_tutor'] = 'secondary_tutors_id = ' . $secondary_tutor . ' ' : null;
isset($location_id) ? $built_query['location'] = 'location_id = ' . $location_id : null;
isset($lesson_id) ? $built_query['lesson'] = 'lesson_id = ' . $lesson_id : null;
// Flatten the array so we can create a query and add the word ADD in between each element.
$built_query = implode(" AND ", $built_query);
// Run the query
$current_events = Calendar::whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')->whereRaw($built_query)->with('lessons', 'leadtutor', 'secondarytutor')->get();