Show items not in table - mysql

I am trying to return a list of brands that do not have a specific product ID.
Using the following SQL:
SELECT Brands
FROM AllCustomers
WHERE ProductID NOT IN (SELECT ProductID WHERE ProductID ='10235')
Is there a more effective way to do this as it keeps timing out?

Given your subquery has the explicit ProductID in the where clause and the subquery is only returning the ProductID you could simplify your query to be
SELECT Brands FROM AllCustomers WHERE ProductID <> '10235';

If you already have product ID, then why are you adding sub query ?
Simply write down query like:
SELECT `Brands` FROM `AllCustomers` WHERE `ProductID`!='10235'

Assuming you will actually use the same syntax for other queries, you didn't specify a table in your sub-query.
Eg, if we are getting the filtered records of ProductID from the same table, it should be:
SELECT Brands FROM AllCustomers WHERE ProductID NOT IN (SELECT ProductID FROM AllCustomers WHERE ProductID ='10235')

Related

Write a SQL query to select all products with pagination

Two tables are given. In one table we have following columns
productid
product_title
priority
categoryid
and in second table we have
id
productid
color
selling_price
stock
Write a SQL query to select all products in category id 306 with pagination
Lets say Result set would have 1000+ such products so we want to paginate the
results. In a single request only 100 result need to be return
This seems to be a text-book problem.
Still trying to answer. The problem statement is not a straight problem. Here is my answer:
Select Top 100 * from table_2 where productid in (Select (productid, product_title, priority) from table_2 where (categoryid = 306));
For paging please read the link :
MySQL Data - Best way to implement paging?
but your query is:
Select
T1.productid
,T1.product_title
,T1.priority
,T1.categoryid
,T2.id
-- ,T2.productid
,T2.color
,T2.selling_price
,T2.stock
from First_Table T1
inner join to Second_Table T2 on T1.productid=T2.productid
where T1.categoryid=306

Select entries appearing in all results from another query

I have a single table.
This table has 2 fields, product IDs and Store IDs. The same product ID can exist with many different Store IDs.
I need to find the products (if any) that are common across all the stores.
I'm having difficulty constructing the correct query, any advice?
You can check distinct store ids count with product id. If distinct Store ids count equal to total stores that will be the product ids you want.
SELECT productID, count(DISTINCT StoreID) as stroes FROM [Table name] GROUP BY productID
HAVING COUNT(DISTINCT StoreID) = (SELECT COUNT(DISTINCT StoreID) FROM [Table name] );
I'm sure you'll get many better answers, but it sounds like you are wanting the reverse of the distinct clause, not sure if this will work though:
SELECT NOT DISTINCT [Product_ID]
FROM TABLENAMEHERE
You could sue count(distinct productID)
select productID
from my_table
group by productID
having count(distinct productID) = (
select count(distinct store)
from my_table )

MYSQL - Count and GROUP from table1 and get info from table2

I am struggling with a MySQL query which I cant get to work as I want.
In table1 I have co_id, name, code, product, logindate.
in table2 I have pr_id, productname, productno, price.
I want to count and group the PRODUCT from table1, so I can see how many that have picked for example product 1,2,3 etc.
But when I list the result on the page I will need productname, and productno for each id number in the GROUP search. table1.product is joined with table2.pr_id
This is what I have so far, but I think I am missing something with INNER JOIN or similar, right?
SELECT
codes.pickedgift,
products.productno,
products.productname,
COUNT(codes.pickedgift) as num
FROM
codes,
products
GROUP BY codes.pickedgift
ORDER BY codes.pickedgift
you missing the join condition, when you join 2 tables you should link primary key in table1 to its foreign key in another table, so your query can be:
SELECT
codes.pickedgift,
products.productno,
products.productname,
COUNT(codes.pickedgift) as num
FROM
codes INNER JOIN products ON codes.product = products.pr_id
GROUP BY codes.pickedgift
ORDER BY codes.pickedgift
You should use a sub-select for this query.
-- assuming I have your table structure correct.
SELECT p.productno, p.productname, num
FROM (SELECT codes.pickedgift, COUNT(codes.pickedgift) as num
FROM codes
GROUP BY codes.pickedgift) g
JOIN products p ON p.id = g.pickedgift
ORDER BY g.pickedgift
The other thing you have to make sure of is if you're using a group-by, the fields in your select must either be the fields in the group by, or aggregates. MySQL let's you include columns that are not part of the group-by / aggregate, it becomes ambiguous as to which value productno and productname should be represented, which is why I opted for a sub-select instead.

Using DISTINCT and COUNT together in a MySQL Query

Is something like this possible:
SELECT DISTINCT COUNT(productId) WHERE keyword='$keyword'
What I want is to get the number of unique product Ids which are associated with a keyword. The same product may be associated twice with a keyword, or more, but i would like only 1 time to be counted per product ID
use
SELECT COUNT(DISTINCT productId) from table_name WHERE keyword='$keyword'
I would do something like this:
Select count(*), productid
from products
where keyword = '$keyword'
group by productid
that will give you a list like
count(*) productid
----------------------
5 12345
3 93884
9 93493
This allows you to see how many of each distinct productid ID is associated with the keyword.
You were close :-)
select count(distinct productId) from table_name where keyword='$keyword'
FYI, this is probably faster,
SELECT count(1) FROM (SELECT distinct productId WHERE keyword = '$keyword') temp
than this,
SELECT COUNT(DISTINCT productId) WHERE keyword='$keyword'
What the hell of all this work anthers
it's too simple
if you want a list of how much productId in each keyword here it's the code
SELECT count(productId), keyword FROM `Table_name` GROUP BY keyword;
SELECTING DISTINCT PRODUCT AND DISPLAY COUNT PER PRODUCT
for another answer about this type of question, this is my way of getting the count of product based on their product name using distinct. Here a sample:
List of Product (SQLFiddle):
select * FROM Product
Product Name Count Using Distinct (SQLFiddle):
SELECT DISTINCT(Product_Name),
(SELECT COUNT(Product_Name)
from Product WHERE Product_Name = Prod.Product_Name)
as `Product_Count`
from Product as Prod
Optimized version without Distinct:
SELECT Product_Name, COUNT(Product_Name) AS `Product_Count`
FROM Product
GROUP BY Product_Name
Isn't it better with a group by?
Something like:
SELECT COUNT(*) FROM t1 GROUP BY keywork;

MYSQL GROUP BY multiple and get count

I would like to group products by their associated quoteId and get only the unique products from a table like below:
On top of doing this I would like to add a column that shows the count of how many of that product was in the associated quote similar to the following:
This is beyond my MYSQL skills and I was wondering if I could get a hand figuring out what would I have to do for a query to achieve this?
You can use the aggregation function count() and group by productid, quoteid
select productid, quoteid, count(*)
from my_table
group by productid, quoteid
Take a look at following ...
SELECT
productid, quoteid, COUNT(*) AS count
FROM
product
GROUP BY
productid, quoteid
HAVING
COUNT(*) > 1