MySQL query to find customers who have made 0 orders / no orders
Tag : mysql , By : user122937
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I would like to find those customers, who have not made any orders yet. , How about SELECT c.*
FROM Customers c LEFT JOIN
Orders o ON c.CustomerID = o.CustomerID
WHERE o.CustomerID IS NULL
SELECT c.*
FROM Customers c
WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerID = c.CustomerID)
|
magento orders list query
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , This code uses the "Magento way" and accesses the data through the Model layer which insulates you from changes in the table structure (e.g. flat vs EAV). Create a new PHP file containing this skeleton code in the root of your Magento install (if elsewhere update the path for the first require statement). This gives you some examples of how to add attributes to the collection, you should be able to follow the examples to add more if required. It shows you how to filter by attributes, and sort by attributes. Examples also for echo'ing out the fields that you need. require_once 'app/Mage.php';
umask(0);
Mage::app('default');
$orders = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
->joinAttribute('billing_street', 'order_address/street', 'billing_address_id', null, 'left')
->joinAttribute('billing_company', 'order_address/company', 'billing_address_id', null, 'left')
->joinAttribute('billing_city', 'order_address/city', 'billing_address_id', null, 'left')
->joinAttribute('billing_region', 'order_address/region', 'billing_address_id', null, 'left')
->joinAttribute('billing_country', 'order_address/country_id', 'billing_address_id', null, 'left')
->joinAttribute('billing_postcode', 'order_address/postcode', 'billing_address_id', null, 'left')
->joinAttribute('billing_telephone', 'order_address/telephone', 'billing_address_id', null, 'left')
->joinAttribute('billing_fax', 'order_address/fax', 'billing_address_id', null, 'left')
->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_street', 'order_address/street', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_company', 'order_address/company', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_city', 'order_address/city', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_region', 'order_address/region', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_country', 'order_address/country_id', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_postcode', 'order_address/postcode', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_telephone', 'order_address/telephone', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_fax', 'order_address/fax', 'shipping_address_id', null, 'left')
->addFieldToFilter('status', array("in" => array(
'complete',
'closed')
))
->addAttributeToFilter('store_id', Mage::app()->getStore()->getId())
->addAttributeToSort('created_at', 'asc')
->load();
foreach($orders as $order):
echo $order->getIncrementId().'<br/>';
echo $order->getShippingTelephone().'<br/>';
endforeach;
|
How do I display a list of selected Orders and a list of unselected Orders on my edit Delivery screen
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Your Edit() method just needs to get all available orders in addition to your Delivery data model and then populate you view model with all orders and set the IsSelected property based on currently selected Orders. You already have a view model so do not use the data model. You have not shown you data models but I'm assuming Delivery contains a property ICollection Orderspublic ActionResult Edit(int? id)
{
....
// Get your data model
Delivery delivery = db.Deliverys.Find(id);
....
// Get all available orders (see note below)
var orders = db.Orders.Where(o => o.Expected_Date == DateTime.Today).Select(o => new OrderVM()
{
ID = o.OrderID,
Name = o.Hospital.Name
}).ToList();
// Mark selected orders based on data model
// Because you have not shown your data models, the following is a best guess
IEnumerable<int> selectedOrders = delivery.Orders.Select(o => o.OrderID);
foreach (var order in orders)
{
if (selectedOrders.Contains(order.ID))
{
order.IsSelected = true;
}
}
// Initialize the view model
DeliveryVM model = new DeliveryVM()
{
ID = delivery.DeliveryID,
DriverID = delivery.DriverID,
Orders = orders,
DriverList = new SelectList(db.Drivers, "DriverID", "First_Name")
}
return View(model);
}
HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(DeliveryVM model)
{
// similar to your Create() method, except that you are saving an existing
// Delivery rather than adding one (and you may not need to save it unless
// the Driver has changed) and updating the collection of Orders based on
// the IsSelected property
}
|
Mysql Query: LEFT JOIN List all orders and "balance due" for a customer, if no orders, just list customer. 3 T
Tag : mysql , By : Andrew L.
Date : March 29 2020, 07:55 AM
wish of those help do an additional LEFT JOIN, but to a sub-query on payments grouped by order. So, if it finds a record in the prequeried result, you are good to go. Also, I changed to using table "aliases" for shorter readability, especially if table names get long, or you have to join multiple times to the same table in a query. SELECT
c.cust_num,
coalesce( o.order_id, 0 ) as Order_ID,
coalesce( o.invoice_amount, 0 ) as InvoiceAmount,
coalesce( Prepaid.TotalPaid, 0 ) as TotalPaid,
coalesce( o.invoice_amount - coalesce( PrePaid.TotalPaid, 0 ), 0) as BalanceDue
FROM
customers c
LEFT JOIN orders o
ON c.cust_num = o.cust_num
LEFT JOIN
( select
p.order_id,
sum( p.amount ) as totalPaid
from
payments p
group by
p.order_id ) as PrePaid
on o.order_id = PrePaid.order_id
|
LINQ Query - Display customers with their number of orders including the customer with no orders
Tag : chash , By : mobi phil
Date : March 29 2020, 07:55 AM
help you fix your problem You basically need to do a LEFT JOIN between your Customer table and Customer Order table and then do a group by on that result to count the orders for each customer. Assuming you have a class like this public class CustomerOrder
{
public int CustomerId { set; get; }
public int? OrderId { set; get; }
}
var usersWithCount = (from c in db.Customers
join o in db.Orders on c.CustomerId equals o.CustomerId
into result
from sub in result.DefaultIfEmpty()
select new CustomerOrder{ OrderId= sub!=null ? sub.OrderId :(int?) null,
CustomerId = u.CustomerId }
) // Here we have the left join result.
.GroupBy(g => g.CustomerId , (k, orders) => new {
CustomerId = k,
OrderCount = orders.Count(t=>t.OrderId !=null)}).ToList();
|