MYSQL Select all that do not equal something - mysql

Im having a bit of difficulty with one equation I can't seem to figure out or find on here. I am trying to do the following;
I have 2 tables, products and teamproducts. There foreign key is productid. I am trying to select the products from the products table that have not been registered to the teamproducts table. I am also using a teamcode that in teamproducts. An example below;
products Table (productid | productname)
1001 | product 1
1002 | product 2
1003 | product 3
1004 | product 4
1005 | product 5
etc
teamproducts Table (teamcode | productid)
teamcode1 | 1001
teamcode1 | 1002
What I want to do is select all the products that aren't in the teamproducts page (so in this example product 3 and on)
I have tried the following;
SELECT productname FROM products p, teamproducts tp WHERE teamcode = teamcode1 AND p.productid != tp.productid
and other variations I have seen but have not come up with the right line. Any help please.

Untested, but I think this should do the job.
SELECT products.productname
FROM products
LEFT JOIN teamproducts
ON teamproducts.teamcode = 'teamcode1'
AND teamproducts.productid = products.productid
WHERE teamproducts.productid IS NULL;

I think you have to write teamcode1 between quotes.
Something like this:
SELECT productname FROM products p, teamproducts tp WHERE teamcode = 'teamcode1' AND p.productid != tp.productid

You can use a not in clause
select * from products
where id not in (select productid from teamproduct);

What you are looking for is LEFT JOIN, with an extra condition that eliminates the null results from the right table:
SELECT * FROM products
LEFT JOIN teamproducts USING (productid)
WHERE teamproducts.productid IS NULL; -- Add other conditions as needed
EDIT Adding 'teamcode' condition:
SELECT * FROM products
LEFT JOIN teamproducts USING (productid)
WHERE teamproducts.productid IS NULL
AND teamproducts.teamcode = 'teamcode1'; -- Add other conditions as needed
More about JOIN, and different types of it: http://dev.mysql.com/doc/refman/5.7/en/join.html

Related

SQL Select data and if loop

I must SELECT and JOIN data from tables. For example I have table:
PRODUCTS: id | productName
PRICES: id | idProduct | price1
PRICES_GROUPS: id | idProduct | price2
When idProducts exist on table PRICES_GROUPS I must get "price2" when not exist I must get "price1" from tabel PRICES
I use mySQL. Any idea to solution this problem?
In you case you can try
select p.*,COALESCE(pg.price2,pc.price1) as price from PRODUCTS p left join PRICES_GROUPS pg on pg.idProduct = p.id left join PRICES pc on pc.idProduct = p.id;

Select between range of values in 2 tables

I have 2 tables
-- Purchases --
Id | IdProvider | Date | Observations
and
-- PurchasesDetails --
Id | IdProduct | Quantity | Price
where Purchases.Id = PurchasesDetails.Id
I want to make a SQL query where it returns all the purchases between a range of price (The price info is on PurchasesDetails table -> Quantity*Price)
For example: Get all the purchases that costed more than 0$ but less than 500$
I've tried this:
SELECT * FROM Purchases INNER JOIN PurchasesDetails ON Purchases.Id = PurchasesDetails.Id WHERE Purchases .Id sum(PurchasesDetails.Price*PurchasesDetails.Quantity) BETWEEN 0 AND 500
But it doesn't work. Seems like I'm missing a condition to link up all the PurchasesDetails with the same Id
I think it's a really easy task but I'm stuck there for few hours so all the help are welcome!!
Is this what you want?
SELECT p.*,
SUM(pd.Price * pd.Quantity) as purchase_total
FROM Purchases p INNER JOIN
PurchasesDetails pd
ON p.Id = pd.Id
GROUP BY p.Id
HAVING purchase_total BETWEEN 0 AND 500;
Note that SELECT p.* is fine with GROUP BY p.id, assuming that id is unique in Purchases.

MySQL - return single record by joining multiple tables with multiple WHERE/OR clauses

Did many searches and were able to write all the JOIN queries except this one. I have 3 tables, which look like:
TABLE - accounts
account_id | account_email
1 | aa#bb.com
2 | cc#dd.com
TABLE - products
product_id | product_name
1 | name1
2 | name2
TABLE - licenses
license_id | account_id | product_id | license_code
1 | 1 | 1 |
2 | 1 | 2 |
3 | 0 | 1 | abc123
I know account_email, product_id and license_code (this one can be empty) variables, and need to check if client has license for selected product (searching by account_email or license_code).
The problem is that account_id sometimes can be 0 (in other words, client has license, but client's profile is not stored in the accounts table).
Trying to use this one, but it returns wrong (duplicated) results:
SELECT * FROM licenses
INNER JOIN products ON licenses.product_id=products.product_id AND products.product_id='X'
INNER JOIN accounts ON licenses.account_id=accounts.account_id AND accounts.account_email='XYZ' OR licenses.license_code='ZZZ'
The question: how do I rewrite query, so I can find a record by account_email or license_code field? Put simply, if account_id is not 0 (profile exists), I should see data from 3 tables (accounts, products, licenses). If account_id is 0, I should see data from 2 tables (values from accounts table should be displayed as empty/null).
Needless to say, account_email and license_code fields are unique.
You just need to adjust the parentheses. However, I would use a separate WHERE clause:
SELECT *
FROM licenses l INNER JOIN
products p
ON l.product_id = p.product_id INNER JOIN
accounts a
ON l.account_id = a.account_id
WHERE p.product_id = 'X' AND
(a.account_email='XYZ' OR l.license_code = 'ZZZ')
You can keep the filters in the ON clauses -- there is nothing wrong with that. I just prefer to separate join conditions from filtering conditions. The important part are the parentheses, so the ON clause is not interpreted as:
ON (l.account_id = a.account_id AND a.account_email = 'XYZ') OR
licenses.license_code = 'ZZZ'
This is the interpretation without parens.
After some more code modification and testing, here's the code which works perfectly. It's a modified Gordon Linoff's code (original code didn't work when only license_code was known, and email address wasn't known). So most of credits go to Gordon Linoff for his efforts.
SELECT *
FROM licenses l JOIN
products p
ON l.product_id = p.product_id LEFT JOIN
accounts a
ON l.account_id = a.account_id
WHERE p.product_id = 'X' AND
(a.account_email='XYZ' OR l.license_code = 'ZZZ')

Matching items in one table that don't match in a subset of a second table

Suppose I have a Product table, and a
id product
1 Apple
2 Bag
3 Cat
4 Ducati
and a Cart table
id user_id product_id
1 1 2
2 1 3
3 2 1
4 3 1
So, I want to look at a particular user and see what he/she does NOT have in their Cart.
In other words, in the above example
SELECT ...... WHERE user_id=1 .....
would return Apple and Ducati because User 1 already has Bag and Cat.
(This may well duplicate another question but there are so many variations I couldn't find the exact match and put in these simple terms may help)
Perform a left join from product to all products purchased by user1, which can be retrieved with a subselect in the join. This will cause all product id's that are not in user1's care to have null product ids. The where clause will select all null product id's meaning they will not have been in a users cart, essentially filtering purchased items.
select p.name
from product p
left join (select product_id, user_id
from cart where user_id = 1)
c
on p.id = c.product_id
where c.product_id is null;
SQL Fiddle: http://sqlfiddle.com/#!2/5318eb/17
Select
*
From Product p
Where p.id Not In
(
Select c.product_id
From Cart c
Where User ID = ____
)
SELECT product FROM product_table
WHERE product NOT IN
(SELECT product_id FROM cart_table WHERE user_id = 1);
This will give you all product for all users which are not in there cart.
select c.user_id,a.Product
from cart c Cross Join product a
left Join
cart b on b.product_id=a.id and c.user_id=b.user_Id
where b.product_id is null
group by c.user_id,a.Product
Sql Fiddle Demo

mysql error in my query

i have to check in my products i am selling (mostly gaming consoles and games)
i want to see which products has which categories and this is my query:
select * From products left join products_categories on (product_id=id) ;
+------+------+------------+-------------+----------
| id | name | product_id | category_id | and more
+------+------+------------+-------------+----------
| 4 | Xbox | 4 | 2 |
| 5 | PS3 | 5 | 2 |
| 7 | BAD | NULL | NULL |
etc...
+------+------+------------+-------------+---------
here i have a product (#7 - BAD) that i don'T want to see since i removed the category,
I don't want to see the product without categories?
The LEFT JOIN command is used to combines null matching rows which are
stored in related tables In order to join these tables, the join table
require a common field (commonly called foreign key) from the left
table. This type of join requires keywords ON or USING.
Example:
SELECT *
From products
LEFT JOIN products_categories ON (product_id=id)
WHERE product_id IS NOT NULL;
Or you can use the INNER JOIN:
The JOIN or INNER JOIN command is used to combines non-null matching
rows which are stored in related tables In order to join these tables,
the join table require a common field (commonly called foreign key)
from the left table. This type of join requires keywords ON or USING.
Example:
SELECT * From products INNER JOIN products_categories ON (product_id=id);
Now, I would recommend to add a flag for inactive or active product, this way you don't need to remove the categories for a product if it's inactive. This way, if you want to re-activate it, simply turn the flag back to 1 or whatever flag you use.
Example:
SELECT *
FROM products
INNER JOIN products_categories ON (product_id=id)
WHERE products.is_active = 1;