mysql multiple values in 2 rows - mysql

I have a table with those fields:
phID, ProductID
The phID is the pharmacy id and the ProductID is the product id.
One pharmacy has multiple products
1 product is to multiple pharmacies
Example:
phID ProductID
-----------------
1001 9
1001 10
1001 11
1004 9
1004 12
1004 14
1004 11
The query that I want is so I can get all the phID that has the same product.
I have this query:
SELECT phID, ProductID
FROM ph_pd
WHERE ProductID IN (9,10,11)
I want the results to be
1001 9
1004 9
1001 11
1004 11
or just
1001
1004

If you want to get distinct phID which are fall under the certain productID, you have to use DISTINCT...
SELECT distinct phID FROM ph_pd WHERE ProductID IN (9,10,11)

You can use :
SELECT p.phID , p.ProductID FROM ph_pd p
WHERE (select count(*) from ph_pd where ProductID = p.ProductID) >= 2
ORDER BY ProductID ;

SELECT phID
FROM tableName
WHERE productID IN (9,10,11)
GROUP BY phID
HAVING COUNT(DISTINCT productID) = 2 -- since there are two stores
SQLFiddle Demo

Related

How to join to single row in one to many relationship in SQL?

I want to join one to many table with single row on many table by limit 1 and order by create date
tbl_cart :
id fullname
1 myname1
2 myname2
3 myname3
tbl_cart_status:
id cart_id status created_at
1 1 33 2018-09-20
2 1 34 2018-09-23
3 2 34 2018-09-21
4 1 100 2018-09-25
5 2 35 2018-09-29
How can i get output with sql like this:
I want to get lastest status of my cart by ordered with created_at column
myname cart_id status created_at
myname1 1 100 2018-09-25
myname2 2 35 2018-09-29
Think filtering for this type of query:
select c.name, cs.*
from tbl_cart c join
tbl_cart_status cs
on c.id = cs.cart_id
where cs.created_at = (select max(cs2.created_at)
from tbl_cart_status cs2
where cs2.cart_id = cs.cart_id
);

MYSQL Stuck Generating temp table (massive query)

I have 4 tables (1 to many):
Dont say anything about that "email" relation. It is how my developer boss built it years ago.
EMPLOYEES (+-50 results)
------------------------------------------------
id name
1 EmpName 1
2 EmpName 2
CUSTOMERS (+50k results)
------------------------------------------------
id name email employee_assigned
1 John john#doe.com 12
2 Donald donald#duck.com 6
INTERESTS_CATEGORIES (+650k results)
------------------------------------------------
id customer_email category_id
1 john#doe.com 97
2 john#doe.com 13
3 donald#duck.com 56
4 donald#duck.com 126
5 donald#duck.com 45
INTERESTS_PRODUCTS (+650k results)
------------------------------------------------
id customer_email product_id
1 john#doe.com 78
2 john#doe.com 23
3 donald#duck.com 19
4 donald#duck.com 56
5 donald#duck.com 45
So I need to filter the customers by their assigned employee and their interests.
And here is the query:
SELECT
*
FROM
(
SELECT
customers.id AS 'id',
customers.name AS 'first_name',
customers.email,
employees.id AS 'employee_id'
FROM
customers,
employees
WHERE
employees.id = 2
AND
customers.employee_assigned = employees.id
) AS myCustomers
LEFT JOIN interests_categories
ON interests_categories.customer_email = myCustomers.email
LEFT JOIN interests_products
ON interests_categories.customer_email = myCustomers.email
WHERE
(
interests_categories.category_id = 20
OR
interests_categories.category_id = 21
)
GROUP BY myCustomers.email
So, the problem:
If the employee has a low number of assigned customers (like 3) query
is successfull.
If the employee has a medium-high number of assigned customers (over 100) query stucks.
I execute SHOW PROCESSLIST and it is stucked "Generating temp table".
Anyone has idea? :(
Thank you.
Check the indexes on your tables and try this:
SELECT
c.id AS 'id',
c.name AS 'first_name',
c.email,
e.id AS 'employee_id'
ic.*,
ip.*
FROM customers c
JOIN employees e
ON c.employee_assigned = e.id
LEFT JOIN interests_categories ic
ON ic.customer_email = c.email
LEFT JOIN interests_products ip
ON ic.customer_email = c.email
WHERE
(
ic.category_id IN (20,21)
AND e.id = 2
)
GROUP BY myCustomers.email
Incidentally, a less dumb design might look like as follows. If it was me, I'd start with this, and provide properly representative CREATE and INSERT statements accordingly. Also, I'm curious about where category_id comes from - because that's potentially an area for further optimization.
EMPLOYEES
------------------------------------------------
employee_id name
6 EmpName 1
12 EmpName 2
CUSTOMERS
------------------------------------------------
customer_id name email employee_assigned
1 John john#doe.com 12
2 Donald donald#duck.com 6
INTERESTS_CATEGORIES
------------------------------------------------
customer_id category_id
1 97
1 13
2 56
2 126
2 45
INTERESTS_PRODUCTS
------------------------------------------------
customer_id product_id
1 78
1 23
2 19
2 56
2 45

I want to sum Amount and Count No. of rows in mysql

I have a table like this
id productName amount
1 Abc 10
2 xyz 20
3 Abc 10
4 Abc 10
5 Abc 10
6 Abc 10
7 xyz 20
8 xyz 20
9 xyz 20
10 xyz 20
I want to sum Amount and Count No. of rows for each products in mysql. Output like below
productName noQty totalAmount
ABC 5 50
xyz 5 100
select product_name,sum(amount)as totalamount,count(productName)as noQty
from table_name
group by productname
by using count you can get COUNT from your table and using SUM you can get sum of all same product name.
SELECT productName,
count(productName) AS noQty,
SUM(amount) AS totalAmount
FROM tableName
GROUP BY productName
select productname,count(productname) as qty,sum(amount)
from table_name
group by productname

MySQL group by min() and limit output

This is the current SQL query I am working with:
SELECT Merchant.Product, Merchant.Name, Merchant.Price
FROM a_table AS Merchant
JOIN
(
SELECT Product, MIN(Price) AS MinPrice
FROM a_table
GROUP BY Product
) AS Price
ON Merchant.Product = Price.Product
AND Merchant.Price = Price.MinPrice
From this data set:
Product Name Price
11 Merch1 19.00
11 Merch2 20.00
11 Merch3 19.00
11 Merch4 19.50
12 Merch1 20.00
12 Merch2 20.00
13 Merch1 17.00
13 Merch3 15.00
The current SQL outputs multiple product records when prices are the same like this:
Product Name Price
11 Merch1 19.00
11 Merch3 19.00
12 Merch1 20.00
12 Merch2 20.00
13 Merch3 15.00
I want to Group By product and display the lowest price with corresponding row data. If two prices are the same on a product, use first record found.
Trying to get this result:
Product Name Price
11 Merch1 19.00
12 Merch1 20.00
13 Merch3 15.00
You don't need any joins to do this.
If you are looking to get the min price for every product by merchant you can do this:
SELECT Product, Name, MIN(Price) as MinPrice
FROM a_table
GROUP BY Product, Name
If you just want the min price of a product regardless of merchant you can do this:
SELECT Product, MIN(Price) as MinPrice
FROM a_table
GROUP BY Product
Here is the query that finally worked for my needs...
http://sqlfiddle.com/#!9/c8937c/35/0
SELECT emp2.product,
emp1.name,
emp2.MinPrice
FROM (
SELECT product,
Min(price) as MinPrice
FROM Merchant
GROUP BY product
) as emp2 JOIN Merchant as emp1 ON emp1.price = emp2.MinPrice
GROUP BY product;

Mysql sum of field values - count , with group by , order by , limit function

I have Table user_views
My Tablet Sample data
Id | product_id | category | user_id | sitename| price | click_count | created_date
1 10 watch 102 ebay 820 1 2014-08-18 13:56:05
2 10 watch 102 amazon 750 1 2014-08-19 13:56:05
3 10 watch 102 amazon 740 1 2014-08-19 18:00:05
4 10 watch 102 ebay 940 1 2014-08-25 08:00:00
5 10 watch 102 amazon 640 5 2014-08-25 08:10:10
6 10 watch 102 ebay 580 3 2014-09-25 18:10:10
7 10 watch 102 amazon 980 5 2014-10-05 12:20:40
I want the total count of the user visited this product
My Query
"select Id , proudct_id , category , user_id , count(click_count) as cnt from user_view where user_id =102 group by product_id order by rand() limit 0,10"
But the output is showing only one count
OUTPUT
Id | product_id | category | user_id | cnt
1 10 watch 102 1
EXPECTED OUTPUT IS
Id | product_id | category | user_id | cnt
1 10 watch 102 17
You should be using sum aggregate function instead of count which will count number of rows. So your query should be something like:
select Id , product_id , category , user_id , SUM(click_count) as sum ...
Try this:
select Id , proudct_id , category , user_id , SUM(click_count) as cnt
from user_view
where user_id =102
order by rand()
limit 0,10
group by product_id
COUNT() always returns the number of items, not the sum of values. So use SUM().
"select Id , proudct_id , category , user_id , count(click_count) as cnt from user_view where user_id =102 group by user_id, product_id order by rand() limit 0,10"