Ok, I wasnt sure how to title this.
I have 2 tables, one with products and one with merchants.
Some products can be sold by 2 or more merchants.
The products table has a column called 'product_merchant_id' with 1 or more references in it, like so.. '1237, 38272, 3738'
The id's are related to whats in the 'merchant_id' column in the 'merchants' table.
Merchant Table (merchants)
mer_id | merchant_id | merchant_name
-------------------------------------
1 | 1237 | Merchant One
2 | 38272 | Merchant Two
3 | 3738 | Merchant Three
Product Table (products)
product_id | product_merchant_id | product_name
------------------------------------------------------------
1 | 1237, 38272 | Sample Product One
2 | 1237, 3738, 38272 | Sample Product Two
3 | 3728 | Sample Product Three
So, basically, if I was querying product_id 2, I want to be able to pull 2 rows with the merchant_id's of 1237 & 38272 from the merchant table and loop them in my template, something like...
<div>
<p>Merchant Name: Merchant One</p>
<p>Merchant ID: 1237</p>
</div>
<div>
<p>Merchant Name: Merchant Two</p>
<p>Merchant ID: 38272</p>
</div>
The solution is to change your table structure. Remove the product_merchant_id column in the Product table, then create a new table, called product_merchants, with two columns: product_id and merchant_id. It will look something like:
product_id | merchant_id
--------------------------
1 | 1237
1 | 38272
2 | 1237
2 | 2728
2 | 38272
3 | 3738
Now, you can use a join to get all the information you need. Something like this should work:
SELECT m.merchant_name, m.merchant_id
FROM merchants m
JOIN product_merchants mp ON m.merchant_id = mp.merchant_id
AND mp.product_id = 2
See demo
Something like this can help:
Select * from merchants where merchant_id in ((select product_merchant_id from products where product_id=2))
I believe this should work.
My advice is you can add one table that like stock table that will be mapped product_id and merchant_id and you will get stock_id it will make easier for development
Related
I have two tables:
cars table which contains:
id | name
1 | Audi
2 | Mercedes
3 | BMW
electric_cars
id | cars_id | non_valid_cars (json)
1 | 1 | [1,3]
2 | 3 | [1]
3 | 2 | [2,3]
How to select all records from the cars table which are not in the non_valid_cars array of ids in the electric_cars column with id cars_id?
Also, I am using Laravel Framework, but I will translate a query into the framework.
You can use a NOT EXISTS condition:
select c.*
from cars c
where not exists (select *
from electric_cars ec
where ec.non_valid_cars::jsonb #> to_jsonb(c.id)
and ec.cars_id = c.id);
Note that the use of jsonb is recommended over json so you might want to change that to avoid the casting.
I have two mySQL database table, I'm using one to store to product information and the other to store the sales information of product sold, where it receive my product_id as the product it sold
product Table example
product_id | prod_name |
------------------------
1 | toyota |
2 | lexus |
3 | wagon |
sale Table example
sale_id | prod_id | qty
------------------------
1 | 1 | 5
2 | 1 | 1
3 | 3 | 2
4 | 1 | 4
5 | 2 | 5
6 | 2 | 1
Now i want the mysqli datebase to tabulate the sum of the most sold product name using php and html table
you can use this query
SELECT
product.product_id,
product.prod_name,
sum(qty) as total
FROM sale
INNER JOIN product
ON
sale.prod_id=product.product_id
GROUP BY
product.product_id
ORDER BY
total DESC
this fiddle if you want to see the results
and you just need to echo the column name if you want to show it to your table on php
My environment:
I have these tables:
Table ___Billing:
|----------|----------|----------|--------------|---------------------|
| BIL_Id | BIL_Item | BIL_Rate | BIL_Quantity | BIL_ApplicableTaxes |
|----------|----------|----------|--------------|---------------------|
| 1 | Hot-Dog | 4.50 | 2 | 3 |
| 2 | Tea | 3.25 | 3 | 3,4 |
|----------|----------|----------|--------------|---------------------|
BIL_Id = the ID for this item in this table.
BIL_Item = the id of the item (referring to the ___SalesTaxes table).
BIL_Quantity = the quantity of the item the customer used.
BIL_ApplicableTaxes = the id of the applicable taxes for this item. Each taxe id is comma separated.
Table ___SalesTaxes:
|----------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|----------|--------------|------------|
| 3 | Tax 1 | 3.50 |
| 4 | Tax 2 | 6.55 |
|----------|--------------|------------|
STX_Id = the ID for this item in this table.
STX_TaxeName = the name of the taxe.
STX_Amount = the amount in percentage of the taxe.
My question:
How is it possible from these two tables, to loop into the ___Billing table in order to get the taxes in percentage I need to applied ?
For example :
For the first row, I should have: 3.50.
For the second row, I should have : 3.50, 6.55.
Hope the question is clear.
Thanks.
If you phrase your query correctly, you can use MySQL's FIND_IN_SET() function to match tax IDs agains the CSV list you have in the BIL_ApplicableTaxes column:
SELECT t1.BIL_Id,
t1.BIL_Rate,
GROUP_CONCAT(COALESCE(t2.STX_Amount, 'NA')) AS tax_due
FROM ___Billing t1
LEFT JOIN ___SalesTaxes t2
ON FIND_IN_SET(t2.STX_Id, t1.BIL_ApplicableTaxes) > 0
GROUP BY t1.BIL_Id
However, you should seriously consider normalizing the ___Billing table and removing the CSV data. Instead, each tax entry should have its own record.
Demo here:
SQLFiddle
I have been working on a product database schema and one of the requirements is that in order to consider a product as discontinued all of the product skus must be set to discontinued.
Product Table:
id
slug
name
One Product to Many ProductSkus
ProductSku Table
id
slug
name
product_id
product_sku_availability_id
One ProductSku to One ProductSkuAvailability
ProductSkuAvaliability
id
slug
name
now I don't really need an exact query, but is there a way to only select products where all productSkus.product_sku_availability are discontinued? here is a sample of what I am asking:
| productId | productSkuId | availabilityId |
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 2 | 3 | 2 |
| 2 | 4 | 1 |
in the above sample I would only want to select the productId = 1 because both product skus have an availabilityId of 1.
If the value of the column availabilityId has got the same value for every productId then are the maximum and minimum value equal. Because of that should a
GROUP BY productId
with
HAVING (MIN(availabilityId) = 1 AND MAX(availabilityId) = 1)
produce the desired result.
I have a mysql table like this one (it's an example):
| ID | Product name | Price | TopProduct |
| 01 | Product 001 | 10.00 | 1 |
| 02 | Product 002 | 15.00 | 0 |
| 03 | Product 003 | 20.00 | 1 |
| 04 | Product 004 | 10.00 | 0 |
Where TopProduct is a flag that the user set on the admin system.
The products that have the flag (TopProduct) set to 1, should appear first on the list and then, after the query bring me all the flagged, it would then show the other products ordered by "ID"
So in the end result i would have a list like this:
Product 003
Product 001
Product 004
Product 002
The question is, how should i put together my query or queries?
Any thoughts on that?
You can use this:
ORDER BY `TopProduct`, `ID` DESC
You can do something like this
SELECT ID, ProductName, Price FROM product ORDER BY TopProduct, ID DESC
It will first order by TopProduct, Then again based on the ID
This can be solved this way:
select ProductName from table
order by TopProduct desc
HOWEVER, this will only work under the condition that TopProduct's max value is 1. I mean, if you can flag that column with a 2 in any row this won't work as that row will appear first. Is that the case?
In case it is, this is the query to sort all 1 at first regardless of the presence of any other number in the column:
select ProductName from table
order by if(TopProdct = 1, 1, 0) desc