I have one report page which displays summarized data of other report.I have used php and mysqli. Let me explain you in deep.
I have a web application of store, where you can add product details. Using these product details you can generate packaging list report of products. And based on the generated packaging list report I need to generate one other report which contains summarized data of the packaging list.
below are my tables:
product table:
id | name | desc_id | purity | style_no | type | duty
1 | ABC | 1 | 18 | TEST123 | R | 100
2 | XYZ | 2 | 14 | TEST456 | B | 80
3 | DEF | 1 | 14 | TEST122 | R | 80
4 | PQR | 1 | 18 | TEST124 | R | 120
5 | HJK | 3 | 18 | TEST134 | B | 300
Description table:
id | descrip
1 | Gold Diamond Ring
2 | Gold Diamond Pendant
3 | Gold Diamond Earring
packaging_master table
id | name
1 | pkg_1
2 | pkg_2
packging_details table
id | pkg_id | prod_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 1 | 4
5 | 1 | 5
I have used below query to generate the packaging list report for specific id, which works correctly.
SELECT id, (SELECT descrip FROM description WHERE id = desc_id ) AS descrip,
style_no, type , purity, duty FROM product WHERE id IN ( SELECT prod_id FROM packaging_list_details WHERE pkg_id =1 ) ORDER BY descrip ASC , purity ASC
which displays below result:
id | descrip | style_no | type | purity | duty
1 |Gold Diamond Ring | TEST123 | R | 18 | 100
4 |Gold Diamond Ring | TEST124 | R | 18 | 120
3 |Gold Diamond Ring | TEST122 | R | 14 | 80
2 |Gold Diamond Pendant| TEST456 | B | 14 | 80
5 |Gold Diamond Earring| TEST134 | B | 18 | 300
Now I want summarized data of above result using query.
Like:
id | descrip | purity | qty | duty
1 |Gold Diamond Ring | 18 | 2 | 220
2 |Gold Diamond Ring | 14 | 1 | 80
3 |Gold Diamond Pendant| 14 | 1 | 80
4 |Gold Diamond Earring| 18 | 1 | 300
How can I achieve this?
You need to use the GROUP_BY statement - See MySql docs for more info.
This will translate the query to such
SELECT d.descrip, p.purity, count(p.purity) as qty, sum(p.duty)
FROM product p
INNER JOIN Description d ON p.desc_id = d.id
LEFT OUTER JOIN packaging_details pg on pg.prod_id = p.id
GROUP BY d.descrip, p.purity
ORDER BY d.descrip desc, p.purity desc
You can also use the sub select methodology you were using, but I prefer using joins. INNER JOIN will link both tables so that all their records are returned. OUTER JOIN will return all rows from the tables on the LEFT of the statement and matches them to values from the tables on the RIGHT.
See a full SQL Fiddle sample.
NOTE: I am not sure where you are getting the values for Id in your sample - Are they simply row numbers?
I think you should rewrite your query using JOINs:
SELECT
P.id
,D.descrip
,P.style_no
,P.type
,P.purity
,P.duty
FROM
packaging_list_details PLD
JOIN
product P ON
(P.id = PLD.prod_id)
LEFT JOIN
description D on
(D.desc_id = P.id)
WHERE
(PLID.pkg_id = 1)
That should give you the same result you already have. To get the totals, you can write a new query, similar to the above:
SELECT
P.id
,D.descrip
,P.type
,P.purity
,COUNT(p.id) as total_products
,SUM(P.duty) as total_duty
FROM
packaging_list_details PLD
JOIN
product P ON
(P.id = PLD.prod_id)
LEFT JOIN
description D on
(D.desc_id = P.id)
WHERE
(PLID.pkg_id = 1)
GROUP BY
P.id
,D.descrip
,P.type
,P.purity
The second query gives you the totals you are looking for.
Related
I have a system which stores peoples outfits. Each outfit contains 1 to many items.
Thus my tables are like this
outfits
+-----------+--------+
| outfit_id | cus_id |
+-----------+--------+
| 1 | 5 |
| 2 | 92 |
+-----------+--------+
items
+---------+-------+-------+
| item_id | name | sku |
+---------+-------+-------+
| 1 | hat | 1111 |
| 2 | pants | 2222 |
| 3 | shirt | 3333 |
| 4 | shoes | 4444 |
+---------+-------+-------+
items_in_outfit
+--------+-----------+---------+
| ino_id | outfit_id | item_id |
+--------+-----------+---------+
| 1 | 1 | 3 |
| 2 | 1 | 2 |
| 3 | 1 | 4 |
| 4 | 2 | 1 |
| 4 | 2 | 3 |
+--------+-----------+---------+
I am given this information:
cus_id=92 wants to create another outfit with the sku's 1111 and 3333
However, this is a duplicate of outfit_id=2. So I want mysql to return me the outfit_id of 2. If its not a duplicate, return nothing. It only counts as a duplicate if the same cus_id is trying to make an outfit which has the exact same sku's already.
If cus_id=5 wants to create another outfit with the sku's 1111 and 3333. It should return nothing (as its not a duplicate) because cus_id=5 doesn't have an outfit with just 1111 and 3333
SELECT o.outfit_id FROM
outfits o JOIN
items_in_outfit iio
ON o.outfit_id = iio.outfit_id JOIN
items i
ON iio.item_id = i.item_id
WHERE cus_id = 92 AND ...
This may help you achieve what you are looking for :
select
cus_id
from(
select
cus_id,
group_concat(sku order by sku asc) as items
from outfits o
join items_in_outfit iio on o.outfit_id = iio.outfit_id
join items i on iio.item_id = i.item_id
group by cus_id
) T
where items = '1111,3333'
SEE DEMO HERE
EDIT : As an outfit can't contain more than 1 instance of the same item, this solution may be better for you as you don't have to order the sku id in your php before :
select
o.outfit_id
from
outfits o
join items_in_outfit iio on o.outfit_id = iio.outfit_id
join items i on iio.item_id = i.item_id
where sku IN (1111, 3333)
group by o.outfit_id having COUNT(*) = 2
SEE SECOND DEMO HERE
It's the 3rd day I'm trying to write a MySQL query. Did lots of search, but it still doesn't work as expected. I'll try to simplify tables as much as possible
System has tkr_restaurants table:
restaurant_id | restaurant_name
1 | AA
2 | BB
3 | CC
Each restaurant has a division assigned (tkr_divisions table):
division_id | restaurant_id | division_name
1 | 1 | AA-1
2 | 1 | AA-2
3 | 2 | BB-1
Then there are meals in tkr_meals_to_restaurants_divisions table, where each meal can be assigned (mapped) to whole restaurant(s) and/or specific division(s). If meal is mapped to restaurant, all restaurant's divisions should see it. If meal is mapped to division(s), only specific division(s) should see it.
meal_id | mapped_restaurant_id | mapped_division_id
1 | 1 | NULL
2 | NULL | 1
3 | NULL | 2
I need to display a list of restaurants and number of meals mapped to it depending on user permissions.
Example 1: if user has permissions to access whole restaurant_id 1 and restaurant_3 (and no specific divisions), then list should be:
AA | 3
CC | 0
(because user can access meals mapped to restaurant 1 + all its division, and restaurant 3 + all its divisions (even if restaurant 3 has no divisions/meals mapped))
Example 2: if user has permissions to access only division_id 1, then list should be:
AA | 1
(because user can only access meals mapped to division 1).
The closest query I could get is:
Example 1:
SELECT *,
(SELECT COUNT(DISTINCT meal_id)
FROM
tkr_meals_to_restaurants_divisions
WHERE
tkr_meals_to_restaurants_divisions.mapped_restaurant_id=tkr_restaurants.restaurant_id
OR tkr_meals_to_restaurants_divisions.mapped_division_id=tkr_divisions.division_id)AS total_meals
FROM
tkr_restaurants
LEFT JOIN
tkr_divisions
ON tkr_restaurants.restaurant_id=tkr_divisions.restaurant_id
WHERE
tkr_restaurants.restaurant_id IN (1, 3)
OR tkr_restaurants.restaurant_id IN (
SELECT restaurant_id
FROM tkr_divisions
WHERE division_id IN (NULL)
)
GROUP BY
tkr_restaurants.restaurant_id
ORDER BY
tkr_restaurants.restaurant_name
However, result was:
AA | 2
CC | 0
I believe I'm greatly over-complicating this query, but all the simpler queries I wrote produced even more inaccurate results.
What about this query:
SELECT
FROM tkr_restaurants AS a
JOIN tkr_divisions AS b
ON a.restaurant_id = b.restaurant_id
LEFT OUTER JOIN tkr_meals_to_restaurants_divisions AS c
ON (c.mapped_restaurant_id = a.restaurant_id OR c.mapped_division_id = b.division_id)
As a Base four your further work. It combine all information into one table. If you add e.g. this:
WHERE a.restaurant_id IN (1, 3)
the result will be
| restaurant_id | restaurant_name | division_id | restaurant_id | division_name | meal_id | mapped_restaurant_id | mapped_division_id |
|---------------|-----------------|-------------|---------------|---------------|---------|----------------------|--------------------|
| 1 | AA | 1 | 1 | AA-1 | 1 | 1 | (null) |
| 1 | AA | 2 | 1 | AA-2 | 1 | 1 | (null) |
| 1 | AA | 1 | 1 | AA-1 | 2 | (null) | 1 |
| 1 | AA | 2 | 1 | AA-2 | 3 | (null) | 2 |
just count the distinct meal ids with COUNT(DISTINCT c.meal_id) and take the restaurant name to get AA: 3 for your example 2
I used a sqlfiddle: http://sqlfiddle.com/#!9/fa2b78/18/0
[EDIT]
Change JOIN tkr_divisions AS b to LEFT OUTER JOIN tkr_divisions AS b
Change SELECT * to SELECT a.restaurant_name, COUNT(DISTINCT c.meal_id)
Add a GROUP BY a.restaurant_name at the end.
Update the SQL Fiddle (new link)
I have the needing to chain some joins queries, and I am not able to get the result that I want.
I have many tables to save some product info:
-Products: Name and description in many languages.
-Products_attributes: Some common attributes of the products like the provider or the buying price.
-Products_locations: Info concerning the locations that sell a certain product, like stock, or the selling price.
-Other important table is the Companies one: some of this companies could be a provider.
Well, this is my query:
SELECT p.id
, p.name nameProduct
, p.reference refProduct
, a.buy_price priceProduct
, l.active
, l.sell_price
, l.stock stockProduct
, c.title p_name
FROM products p
LEFT JOIN products_location l
ON l.product_reference = p.reference
AND l.active = 1
AND l.location_id = 4
JOIN products_attributes a
ON a.product_reference = p.reference
AND p.lang = 'es'
AND a.provider = 6
JOIN companies c
ON a.provider = c.id
AND c.id = 6
What I want to do is get all products of a certain provider, if the location from where is executing the query has this product, the row of the result must return too the concerning at this product<->location, in contrary it must return NULL in the column related to this relation.
At the moment, with this query, all what I am getting is the products of a provider where has a relation between the product<->location (through products_location table).
Any way to do that??
Thank you.
EDIT
An example about what I trying to get could be something like this:
TABLE: Companies
id | Title
1 | SomeName
6 | ProviderName
TABLE: Products
id | reference | name | lang
1 | 11111 | 1_es | es
2 | 11111 | 1_en | en
3 | 22222 | 2nam | es
4 | 33333 | 3nam | es
5 | 44444 | 4nam | es
6 | 55555 | 5nam | es
TABLE: Products_atributte
id | product_reference | buy_price | provider
1 | 11111 | 10 | 6
1 | 22222 | 15 | 6
1 | 33333 | 20 | 6
1 | 44444 | 12 | 1
1 | 55555 | 13 | 1
TABLE: Products_locations
id | product_reference | location_id | sell_price | stock | active
1 | 11111 | 4 | 26 | 10 | 1
1 | 11111 | 5 | 25 | 13 | 1
1 | 22222 | 5 | 20 | 13 | 1
1 | 44444 | 5 | 21 | 1 | 1
1 | 55555 | 5 | 22 | 2 | 1
AND THE RESULT MUST BE SOMETHING LIKE THIS:
nameProduct | refProduct | priceProduct | active | sell_price | stockProduct | p_name
1_es | 11111 | 10 | 1 | 26 | 10 | ProviderName
2nam | 22222 | 15 | NULL | NULL | NULL | ProviderName
3nam | 33333 | 20 | NULL | NULL | NULL | ProviderName
If I use a LEFT JOIN only in the products_locations table, I donĀ“t get the two last rows, and if I use LEFT JOIN with all the tables I get duplicates product references, also I get products provided by other providers (in the example 1-> SomeName).
You were correct to LEFT JOIN the products and products_location tables. However, you used INNER JOIN for the other two tables in the query and I believe that this may be the reason why you are only seeing records which have a relation between product and location. The logic would be that a product which does not have a location also does not have an entry in, for example, the products_attributes table. Hence, the non matching records you want to retain would be filtered off downstream by an INNER JOIN. To remedy this, use LEFT JOIN everywhere:
SELECT products.id,
products.name AS nameProduct,
products.reference AS refProduct,
products_attributesbuy_price AS priceProduct,
products_location.active,
products_location.sell_price,
products_location.stock AS stockProduct,
provider.title AS p_name
FROM products
LEFT JOIN products_location
ON products_location.product_reference = products.reference AND
products_location.active = 1 AND
products_location.location_id = 4
LEFT JOIN products_attributes
ON products_attributes.product_reference = products.reference AND
products.lang = 'es' AND
products_attributes.provider = 6
LEFT JOIN companies AS provider
ON products_attributes.provider = provider.id AND
provider.id = 6
So I have this database
TABLE: rates
ID | FID | TID | RATE
---------------------------
1 | 1 | 2 | 0.3
2 | 1 | 3 | 1.2
3 | 1 | 4 | 4.5
4 | 2 | 1 | 1.3
5 | 2 | 3 | 3.3
6 | 2 | 4 | 4.4
TABLE: currencies
ID | Name | Symbol
---------------------
1 | Euro | E
2 | Pound | P
3 | Dollar | $
4 | CAD | C
So what I tried so far was
SELECT rates.*,
currencies.name,
currencies.symbol FROM RATES
JOIN CURRENCIES ON
(rates.fid = currencies.id)
Which worked but only for 1 column. I could not find a way to add more. Also I want to give a custom output name for each currency. So the final output should be:
ID | FromCurrency (FID) | ToCurrency (TID) | Rate
You need to do multiple joins, and as you are using the same table for both joins, give them an alias.
Something like this:
SELECT rates.ID,
a.name AS 'FromCurrency (FID)',
a.symbol AS 'FID Symbol',
b.name AS 'ToCurrency (TID)',
b.symbol AS 'TID Symbol',
rates.rate
FROM RATES
JOIN CURRENCIES AS a ON
(rates.fid = a.id)
JOIN CURRENCIES AS b ON
(rates.tid = b.id)
Here is a working example
Imagine this tables:
| USER |
|------|
| _id |
| PHOTO |
|-------|
| _id |
| user | - who uploaded it
| RATING |
|--------|
| user | - who vote
| photo | - for which foto
And then I need to extract All photos uploaded by $user And on top of that, what rating the photo has...
for example:
USER
1
PHOTO
50 | 1
51 | 1
52 | 1
RATING
2 | 50
3 | 51
2 | 51
2 | 52
4 | 51
3 | 50
My Expected result for function get_info($id):
$id = 1;
result:
_id | rating
50 | 2
51 | 3
52 | 1
Is there possible one task sql ? thanks
Use join and count with group by
select p._id,count(distinct r.user)
from photo p
join rating r on(p._id=r.photo)
where p.user = 1
group by p._id
Since your photo table has an association with rating table as _id is related to the photo column of rating table so join them, then count each distinct user for each photo
Demo