I have the following table which I will like to get the count of items with the same product name on a separate column
Here is the table
+-----------+-------------+-------+------+
| ProductID | ProductName | Price | URL |
+-----------+-------------+-------+------+
| 1 | Book | 2 | url1 |
| 2 | Pen | 1 | url2 |
| 3 | pencil | 0.5 | url3 |
| 4 | Book | 2 | url1 |
+-----------+-------------+-------+------+
And I will like get the following from the table
+-----------+-------------+-------+------+-------+
| ProductID | ProductName | Price | URL | Count |
+-----------+-------------+-------+------+-------+
| 1 | Book | 2 | url1 | 2 |
| 2 | Pen | 1 | url2 | 1 |
| 3 | pencil | 0.5 | url3 | 1 |
+-----------+-------------+-------+------+-------+
The reason why I need this is because the items need to be rendered on an external application with the count. I do not know how to get the count on another column.
This looks like aggregation:
select min(ProductID) as ProductID, ProductName, Price, URL, COUNT(*)
from t
group by ProductName, Price, URL;
SELECT ProductID
,ProductName
,Price
,URL
,COUNT(ProductName) AS CountProduct
FROM products
GROUP BY ProductName
Related
I am not able to figure out how I can get the following result with one MySQL Query:
I have two tables:
shop_items
| id | description | price | active |
+----+-------------+-------+--------+
| 1 | product_1 | 5 | 1 |
+----+-------------+-------+--------+
| 2 | product_2 | 10 | 1 |
+----+-------------+-------+--------+
| 3 | product_3 | 15 | 0 |
+----+-------------+-------+--------+
inventory_items (the shop_items a user purchased)
| id | item_id | user_id | active |
+----+---------+---------+--------+
| 1 | 2 | 1 | 1 |
+----+---------+---------+--------+
| 2 | 1 | 1 | 0 |
+----+---------+---------+--------+
I want to see all shop_items where active = 1 including a row called purchased = 0 or 1 based on inventory_items -> matching user_id (where user_id = something) and active = 1
Example output based on the data from above tables -> where user_id = 1:
| item_id | price | description | purchased |
+---------+-------+-------------+-----------+
| 1 | 5 | product_1 | 0 |
+---------+-------+-------------+-----------+
| 2 | 10 | product_2 | 1 |
+---------+-------+-------------+-----------+
What query do I need for this output?
Please note: I only need the result from ONE user_id which I can change within the query :)
Test
SELECT shop_items.*, COALESCE(inventory_items.active, 0) purchased
FROM shop_items
LEFT JOIN inventory_items ON shop_items.id = inventory_items.item_id
AND user_id = 1
WHERE shop_items.active = 1
I have two tables: products and meta.
Products table:
+----+----------+
| id | name |
+----+----------+
| 1 | TV |
| 2 | Computer |
| 3 | Freezer |
+----+----------+
Meta table:
+----+------------+-----------+------------+
| id | product_id | meta_key | meta_value |
+----+------------+-----------+------------+
| 1 | 1 | currency | USD |
| 2 | 1 | price | 1100 |
| 3 | 2 | currency | PLN |
| 4 | 2 | price | 9300 |
| 5 | 3 | currency | USD |
| 6 | 3 | price | 1200 |
+----+------------+-----------+------------+
now the following query works fine:
select price.product_id, products.name, price.meta_value as 'price', currency.meta_value as 'currency'
from meta as price
join meta as currency on(price.product_id=currency.product_id and currency.meta_key='currency')
join products on(products.id=price.product_id)
where price.meta_key='price';
result:
+------------+----------+-------+----------+
| product_id | name | price | currency |
+------------+----------+-------+----------+
| 1 | TV | 1100 | USD |
| 2 | Computer | 9300 | PLN |
| 3 | Freezer | 1200 | USD |
+------------+----------+-------+----------+
but the query:
select price.product_id, products.name, price.meta_value as 'price', currency.meta_value as 'currency'
from meta as price, meta as currency
join products on(products.id=price.product_id)
where
price.product_id=currency.product_id
and price.meta_key='price'
and currency.meta_key='currency';
returns: "Unknown column 'price.product_id' in 'on clause'"
Why does that happen ?
Your "from" clause is interpreted as:
from meta as price, (meta as currency join products on (products.id = price.product_id)
So, there is no price.product_id available to the on clause, as it only knows about the meta as currency and products tables.
I have a problem to copy data from one table to another table. There is lots of solution found but my problem is something different. I have two tables sku and shipping_skudetails. I have to copy data form shipping_skudetails to sku, for this m doing this,
INSERT INTO test.sku
SELECT SkuDetailsId as id,
sku,
seller_id as sellerID,
itemName as name,
itemLength as length,
itemWidth as width,
itemHeight as height,
itemWeight as weight,
modeType_id as mode
FROM testdb.shipping_skudetails;
+----+---------+----------+-------------+--------+-------+--------+--------+------+
| id | sku | sellerID | name | length | width | height | weight | mode |
+----+---------+----------+-------------+--------+-------+--------+--------+------+
| 1 | Sample | 1 | SampleItem | 1 | 1 | 1 | 1 | 1 |
| 2 | Sample | 2 | Sample1 | 1 | 1 | 1 | 1 | 1 |
| 3 | SDGS046 | 1 | Shivaprasad | 1 | 1 | 3 | 1 | 2 |
+----+---------+----------+-------------+--------+-------+--------+--------+------+
test.sku
+--------------+---------+-----------+-------------+------------+------------+------------+-----------+
| SkuDetailsId | sku | seller_id | itemName | itemLength | itemWeight | itemHeight | itemWidth |
+--------------+---------+-----------+-------------+------------+------------+------------+-----------+
| 1 | Sample | 1 | SampleItem | 1 | 1.000 | 1 | 1 |
| 2 | Sample | 2 | Sample1 | 1 | 1.000 | 1 | 1 |
| 3 | SDGS046 | 1 | Shivaprasad | 1 | 3.000 | 1 | 1 |
+--------------+---------+-----------+-------------+------------+------------+------------+-----------+
testdb.shipping_skudetails
now the problem is values are copied into different columns,
(like : shipping_skudetails.itemWeight copied into test.width)
I want copy data column to column. not by arrange column in query.
I want a query that copy data by identifying column name.
try this
INSERT INTO shyplite.sku (id, sku, sellerID, name, length, width, height, weight, mode)
SELECT SkuDetailsId as id,
sku,
seller_id as sellerID,
itemName as name,
itemLength as length,
itemWidth as width,
itemHeight as height,
itemWeight as weight,
modeType_id as mode
FROM shyplitelivedb.shipping_skudetails;
For this question I have created a simple example that illustrates what I am asking.
Say I had a table called 'books'
+----+----------------------------+-----------+
| pk | title | author_id |
+----+----------------------------+-----------+
| 1 | The Lost Symbol | 1 |
| 2 | Follow Us Home | 2 |
| 3 | The Man in the High Castle | 3 |
+----+----------------------------+-----------+
(table a)
And another table called 'shops', that had a list of shops that sold each book:
+----+---------+-------------+-------+
| pk | book_id | shop_name | price |
+----+---------+-------------+-------+
| 1 | 1 | WHSmith | 5.00 |
| 2 | 1 | Waterstones | 7.00 |
| 3 | 1 | Amazon | 2.50 |
| 4 | 2 | WHSmith | 4.00 |
| 5 | 2 | Borders | 4.50 |
+----+---------+-------------+-------+
(table b)
If I do a simple select that grabs a book and all of the places it is sold using a join such as:
SELECT
books.*,
shops.shop_name,
shops.price
FROM
books
JOIN shops ON books.pk = shops.book_id
WHERE
book.book_name = "The Lost Symbol"
I would get results such as below:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table c)
However, I would LIKE to receive results like this:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table d)
I.e. the first row is just the result of left outer join and the rest of the results are the the inner join.
An even more desired outcome is:
+------+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+------+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| NULL | NULL | NULL | WHSmith | 5.00 |
| NULL | NULL | NULL | Waterstones | 7.00 |
| NULL | NULL | NULL | Amazon | 2.50 |
+------+-----------------+-----------+-------------+-------+
(table e)
Having shop_name and price concatenated and grouped in a single row seems not to work as it only does the first result from shops instead of all of them, also in my real world scenario, I have punctuation in the data so have to be careful with the separator.
So how would I get the result of table e?
You can use UNION ALL to build the required result set:
SELECT pk, title, author_id, NULL AS shop_name, NULL AS price
FROM books
WHERE books.title = "The Lost Symbol"
UNION ALL
SELECT NULL AS pk, NULL AS title, NULL AS author_id, shops.shop_name, shops.price
FROM books
JOIN shops ON books.pk = shops.book_id
WHERE books.title = "The Lost Symbol"
The first part of the union operation returns the first row of the result, i.e. the book title. The second part returns the rest of the rows, i.e.the shop names.
Demo here
I have four tables, like these:
items
| id | name | category |
-------------------------
| 1 | item1 | toy |
| 2 | item2 | toy |
| 3 | item3 | home |
-------------------------
items2
| id | name | category | size |
--------------------------------
| 1 | itemA | toy | s |
| 2 | itemB | home | l |
--------------------------------
prices
| items.id | price |
--------------------
| 1 | 10 |
| 1 | 15 |
| 2 | 20 |
| 3 | 25 |
| 3 | 20 |
--------------------
prices
| items2.id | price |
--------------------
| 1 | 15 |
| 2 | 50 |
| 2 | 40 |
--------------------
I need to get a result which have both, items and items2, with the MIN of each price. In this example the result should be something like this:
| id | name | category | size | minprice |
-------------------------------------------
| 1 | item1 | toy | null | 10 |
| 2 | item2 | toy | null | 20 |
| 3 | item3 | home | null | 20 |
| 1 | itemA | toy | s | 15 |
| 2 | itemB | home | l | 40 |
-------------------------------------------
I also should be able to ORDER BY minprice, but I'm sure when I know how to join them I can do that too.
Thanks in advance!
I would suggest that you merge the tables items and items2. If a field doesn't apply to an item (such as size in this case), that is what null is for.
I haven't tested that this works but you want something like:
SELECT i.id, name, category, null as size, min(prices.price) from items i
JOIN prices on prices.id = i.id
GROUP BY i.id, i.name, i.category, size
UNION ALL
SELECT i2.id, name, category, size, min(prices2.price) from items2 i2
JOIN prices2 on prices2.id = i2.id
GROUP BY i2.id, i2.name, i2.category, i2.size
From what I can tell, you are over complicating things. You only need one table. For tuples in the item relation where there is no size, simply mark it as null.
This is probably an oversimplification. If prices are "sales" or something similar, you could use a left join to build the relation you are looking for
so
SELECT i.id, i.name, i.category, i.size, p.price as min_price FROM items i LEFT JOIN price p where p.price <= 40 ORDER BY min_price