SQL Select data and if loop - mysql

I must SELECT and JOIN data from tables. For example I have table:
PRODUCTS: id | productName
PRICES: id | idProduct | price1
PRICES_GROUPS: id | idProduct | price2
When idProducts exist on table PRICES_GROUPS I must get "price2" when not exist I must get "price1" from tabel PRICES
I use mySQL. Any idea to solution this problem?

In you case you can try
select p.*,COALESCE(pg.price2,pc.price1) as price from PRODUCTS p left join PRICES_GROUPS pg on pg.idProduct = p.id left join PRICES pc on pc.idProduct = p.id;

Related

Select between range of values in 2 tables

I have 2 tables
-- Purchases --
Id | IdProvider | Date | Observations
and
-- PurchasesDetails --
Id | IdProduct | Quantity | Price
where Purchases.Id = PurchasesDetails.Id
I want to make a SQL query where it returns all the purchases between a range of price (The price info is on PurchasesDetails table -> Quantity*Price)
For example: Get all the purchases that costed more than 0$ but less than 500$
I've tried this:
SELECT * FROM Purchases INNER JOIN PurchasesDetails ON Purchases.Id = PurchasesDetails.Id WHERE Purchases .Id sum(PurchasesDetails.Price*PurchasesDetails.Quantity) BETWEEN 0 AND 500
But it doesn't work. Seems like I'm missing a condition to link up all the PurchasesDetails with the same Id
I think it's a really easy task but I'm stuck there for few hours so all the help are welcome!!
Is this what you want?
SELECT p.*,
SUM(pd.Price * pd.Quantity) as purchase_total
FROM Purchases p INNER JOIN
PurchasesDetails pd
ON p.Id = pd.Id
GROUP BY p.Id
HAVING purchase_total BETWEEN 0 AND 500;
Note that SELECT p.* is fine with GROUP BY p.id, assuming that id is unique in Purchases.

MYSQL Select all that do not equal something

Im having a bit of difficulty with one equation I can't seem to figure out or find on here. I am trying to do the following;
I have 2 tables, products and teamproducts. There foreign key is productid. I am trying to select the products from the products table that have not been registered to the teamproducts table. I am also using a teamcode that in teamproducts. An example below;
products Table (productid | productname)
1001 | product 1
1002 | product 2
1003 | product 3
1004 | product 4
1005 | product 5
etc
teamproducts Table (teamcode | productid)
teamcode1 | 1001
teamcode1 | 1002
What I want to do is select all the products that aren't in the teamproducts page (so in this example product 3 and on)
I have tried the following;
SELECT productname FROM products p, teamproducts tp WHERE teamcode = teamcode1 AND p.productid != tp.productid
and other variations I have seen but have not come up with the right line. Any help please.
Untested, but I think this should do the job.
SELECT products.productname
FROM products
LEFT JOIN teamproducts
ON teamproducts.teamcode = 'teamcode1'
AND teamproducts.productid = products.productid
WHERE teamproducts.productid IS NULL;
I think you have to write teamcode1 between quotes.
Something like this:
SELECT productname FROM products p, teamproducts tp WHERE teamcode = 'teamcode1' AND p.productid != tp.productid
You can use a not in clause
select * from products
where id not in (select productid from teamproduct);
What you are looking for is LEFT JOIN, with an extra condition that eliminates the null results from the right table:
SELECT * FROM products
LEFT JOIN teamproducts USING (productid)
WHERE teamproducts.productid IS NULL; -- Add other conditions as needed
EDIT Adding 'teamcode' condition:
SELECT * FROM products
LEFT JOIN teamproducts USING (productid)
WHERE teamproducts.productid IS NULL
AND teamproducts.teamcode = 'teamcode1'; -- Add other conditions as needed
More about JOIN, and different types of it: http://dev.mysql.com/doc/refman/5.7/en/join.html

Get the lowest price for a product from a table of product prices per store

I am making a website to compare product prices between different stores. I have created a search function where I want to display the current lowest price and this is where I am a bit stuck.
I have a table Products with the basic information and a table product_store with the prizes in different stores for different products. Below is a basic schema of the database for these tables:
+-----------+ +---------------+
| products | | product_store |
+-----------+ +---------------+
| id | | product_id |
| name | | store_id |
+-----------+ | price |
| created_at |
+---------------+
The product_store table has multiple prices for the same product_id and store_id so I can create a price history.
Now I would like to create a query to get all products and their lowest price at the moment. So, you take the price for each store with the highest created_at, and from this collection I want to get the lowest value.
This is what I have tried so far:
select products.*, prices.price
from products
left join (
SELECT p1.product_id, min(p1.price) as price, p1.created_at
FROM product_store p1
WHERE p1.created_at = (SELECT max(created_at) FROM product_store p2 WHERE p2.store_id = p1.store_id
) as prices
on prices.product_id = products.id
I search for the highest created_at per store and product and get the lowest price for these rows. However this gives some very strange results where the prices get mixed up between the products and some products that have prices don't have any in the results.
Can someone help me to create a good query for this problem?
Thanks in advance. :)
Here is one method. It aggregates the product_store table to get the maximum created date for each store. Then it joins it back to that table to get price and finally does an aggregation in the outer query:
select p.*, min(ps.price)
from products p left join
(select ps.product_id, ps.store_id, max(created_at) as maxca
from product_store ps
group by ps.product_id, ps.store_id
) pssum
on pssum.product_id = p.id left join
product_store ps
on ps.product_id = pssum.product_id and
ps.store_id = pssum.store_id and
ps.created_at = pssum.maxca
group by p.id;
Your product_store table is an example of a slowly changing dimension. If it were set up with a eff_date and end_date (effective date and end date) columns, then the query would be easier to write and probably more efficient in terms of performance.

Merge/combine fields from two database tables

I have two tables in my database which I want to join some matching results from.
ps_product (column names): id_product | reference | price | wholesale_price
ps_stock (column names): id_product | quantity
I would like to combine these two tables to get:
Table: id_product | reference | quantity | price | wholesale_price
Both tables have matching id_product values and should be matched, so quantity fits the right reference and so forth. Saved to a csv file if possible.
Running mysql on Debian.
Thank you in advance!
You can do inner join as
select
p.id_product,
p.reference,
s.quantity,
p.price,
p.wholesale_price
from ps_product p
join ps_stock s on s.id_product = p.id_product

joining tables in mysql query?

I would like to run a query on two table "products" and "category", There are 30 record in my product table. product table has a column name category_ids varchar(255),which storing the ids of category in the format like(10,11,12,130,..) for each record of products table. In sort a product can be many categories. category table having a column name parent_id which is the parent category of that category.
I want to list the record with all category of that product.
For example look at one record of the products table having id = 7.
product_Id = 7,
category_ids = '213,215,216',
product_name = 'Ponds',
.....
Means product ponds has three category = category.id = 213, category.id = 215 and category.id = 216.
I want to list here all three records of ponds like in this format :=
product_Id | product_name | category_name | parent_category_name
7 ponds cream chemical
7 ponds medicine chemical
7 ponds powder Ayurvedic
I am trying with this query :-
select
p.id as product_id,
p.product_name,
child.name as category_name,
parent.name as parent_category_name
from category child
left join products p on child.id in(p.category_ids)
left join category parent on parent.id = child.parentid and parent.parentid = 0
where p.id = 7
The above query getting only one record not all three records as above.
What condition and joining in this query will be applied to get result as above described.
Sorry for spending your valuable time.
Any suggestions and ideas would be greatly appreciated.
Thanks a lot.
Try to change ON condition -
LEFT JOIN products p ON child.id IN(p.category_ids)
->
LEFT JOIN products p ON FIND_IN_SET(child.id, p.category_ids)
...because:
SELECT 1 IN ('1,2,3') find_1, 2 IN ('1,2,3') find_2;
+--------+--------+
| find_1 | find_2 |
+--------+--------+
| 1 | 0 | -- 0 !
+--------+--------+
SELECT FIND_IN_SET(1, '1,2,3') find_1, FIND_IN_SET(2, '1,2,3') find_2;
+--------+--------+
| find_1 | find_2 |
+--------+--------+
| 1 | 2 |
+--------+--------+
GROUP_CONCAT function.
example:
SELECT p.product_Id, p.product_name, GROUP_CONCAT(c.category) FROM products p
JOIN category c
ON FIND_IN_SET(child.id, p.product_Id)
GROUP BY p.product_Id;