SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product,stock
WHERE (date BETWEEN '2012-04-10' AND '2012-07-16') AND product.pid=stock.pid
This is my sql query but problem is when execute this query its show single result means my product table contains pid and pname and stock table contains pid,rate,qty and date.
I want to display record between two dates.
My query match with two records. But when i add "AND product.pid=stock.pid" its show only 1 record.
I want to display the product name from product table in the respect of pid of stock table.
Probably some product is not in the stock. Also I recommend you to do a JOIN like this:
SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product
LEFT JOIN
stock
ON product.pid=stock.pid
WHERE (date BETWEEN '2012-04-10' AND '2012-07-16')
I used LEFT JOIN that means to return products no matter if have or nor stock, I think that with this query you will get two Rows.
First try to get it working without the join:
SELECT stock.pid, stock.qty, stock.rate
FROM stock
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16'
If that works, then use an outer to join to add information to each row without removing any rows.
SELECT product.pname, stock.pid, stock.qty, stock.rate
FROM stock
LEFT JOIN product ON product.pid=stock.pid
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16'
The product name will be NULL if no product is found.
Try the below query.
SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product,stock
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16' AND product.pid=stock.pid
Your selection criteria
WHERE ([stock.]date BETWEEN '2012-04-10' AND '2012-07-16')
makes it effectively RIGHT JOIN, only records that have/had stock within date range will be displayed. If you want to display ALL products irrespective of stock than you will need to add OR [stock.]date IS NULL to your selection criteria.
Related
I'm working on a panel to show all orders from our e-commerce site and the way I have the orders set up is to have a row for each order referring to the customer id.
I'm working on showing all open orders on our back-end however the rows are showing multiple rows for the same order if (so if the orderID is 18, and the order has 2 items ordered there a 2 rows all the with the orderID of 18).
I'll include some screenshots below so you have an idea of what's happening.
This is my sql statement to show all open orders:
function GetAllOpenOrders (){
global $conn;
$sql = "SELECT * FROM customer_orders LEFT JOIN ordered_products ON ordered_products.custOrderID=customer_orders.custOrderID WHERE customer_orders.orderOpen=1";
return $result = $conn->query($sql);
}
So again, I want to combine the orders with multiple products and display the total price in the open orders screen.
You need an aggregate query to display the total. Something like:
SELECT sum(prod.totalPrice) as TotalPrice
FROM customer_orders
LEFT JOIN ordered_products ON ordered_products.custOrderID=customer_orders.custOrderID
WHERE customer_orders.orderOpen=1
group by customer_orders.custOrderID
I don't have your database to test that query but that should point you in the right direction.
You need a sub-query where you SUM the order totals and then aggregate. You also can't add the "product ID" to your query otherwise it will by definition break it down into rows for each product ordered.
For example
select
customer_order_id,
sum(product_total_price) as order_total
from
(select
customer_order_id,
product_id,
product_total_price
from table
group by 1,2,3)
group by 1
If you're looking to show the names of the products ordered in your 1 row, I suggest using a CONCAT function for the product names (so it's all in one field) and then you'll have the total in 1 row.
I've two tables level & product, basically I need to find out level.id = 1, but I also need to know if it has product attached to this id
in product table. There is a pdcatid in my product table. Now
the query works only if this id also in product table,
if product table doesn't have this id, it will fail and retrurn empty. How can I show both situations?
if has product, show pdcatid
if no product, show NULL
Here is my query I've tried
SELECT *
FROM level
RIGHT JOIN product on level.`id` = product.`pdcatid`
WHERE level.`id` = 1
Thank You.
Use left join instead of right join.
Try this:
SELECT * FROM level LEFT JOIN product on level.`id` = product.`pdcatid` WHERE level.`id` = 1
I have two tables which displays the contents of the tables according to the table,
I have two table product and sales
pcode is the primary key for product and foreign key to sales.
I need to display sales according to the particular days sale.
My Query is :
$result = mysql_query("SELECT product.pcode,pname,brand,price,oldbal,receipt,total,current,sales.date FROM product,sales WHERE sales.date='$date' AND product.pcode=sales.pcode");
this displays only product which was sold on that particular day, I need to display all product from product table and if they are sold they should display the data or null if its not.
How can i do this ?
What you need is known as LEFT JOIN.
SELECT product.pcode,pname,brand,price,oldbal,receipt,total,current,sales.date
FROM product LEFT JOIN sales
ON product.pcode=sales.pcode
WHERE sales.date=$date
You can use LEFT JOIN
SELECT product.pcode,pname,brand,price,oldbal,receipt,total,current,sales.date FROM product LEFT JOIN sales ON product.pcode = sales.pcode WHERE sales.date='$date';
I've a problem with MySQL query. I'm trying select products info over the order products list, but it are selecting only the first record, being that the products list is a array.
Example: being orders.itens = "10,11,12,13", the "IN" selects only the first ID, "10". How I do to select all ID's, something that sounds like implode function in PHP?
SELECT
orders.id,
products.name,
products.price
FROM
orders,
products
WHERE
products.id IN (orders.itens)
Thank you
You want to do a join:
SELECT o.id, p.name, p.price
FROM orders o join
products p
on find_in_set(p.id, o.items) > 0;
Unfortunately, there is no way to optimize this query. You should created an association/junction table, probably called OrderProducts that contains one row for each product in an order.
I guess you need something like this
select CONCAT('"',CONCAT(Replace("10,11,12,13", ',', '","'),'"')) as str
And use this as the value for in clause
I am building an auction website. Right now, I am building the item description page, that has item details, as well as current bid history. My bids table has a FK of Item_id.
My current query looks something like this:
SELECT bids.Item_id, bids.User_email, bids.Bid_amount, products.*
FROM bids
INNER JOIN products
ON bids.Item_id=products.Item_id;
This returns all of the bid information I need - but also returns the item description for every bid row. I only need the product information once. Is it best to just use two queries on this?
Any help is appreciated
If you need the bids data separately from the products data, then you should use two queries.
One query cannot really be arrange to return different columns for different rows.
SELECT b.Item_id, b.User_email, b.Bid_amount, p.*
FROM bids b
INNER JOIN products p
ON b.Item_id=p.Item_id
WHERE p.Item_id=something;
This will not repeat products..