I have a table in mysql like e.g.
--------------------------
Line | category | product |
==========================
1 | 1 | 500 |
2 | 10 | 500 |
3 | 1 | 510 |
4 | 11 | 510 |
5 | 2 | 520 |
6 | 10 | 520 |
--------------------------
Now I was wondering if its possible to select category from line 2 and 4 because they also exist with the category value 1 in the table.
I tried some stuff like
select
max(categorie),
product
from
products
group by
product
but this brings up all results. even those having a product that has 2 as category.
Expected output is:
| category |
|==========|
| 10 |
| 11 |
------------
I think the easiest way would be self joining the table, so that each product gets matched with the rows of the same product that have 1 as category
select t1.category
from yourTable t1
join yourTable t2
on t1.product = t2.product and
t1.category <> t2.category
where t2.category = 1
Related
I have a table in which I link items to types where each type can have multiple categories. For each combination of type and category, only one item should be linked. However, through some past error of mine, some duplicates have slipped through. I am now trying to write a query that will give me the duplicates but I am not doing a great job or I wouldn't be posting here obviously.
SELECT
item_id,
type_id,
category
FROM itemTypes
WHERE category = 'cat1'
GROUP BY type_id
HAVING COUNT(*) >= 2;
This is what I tried. It does work and gives me the type_ids that are linked to different items. But each type_id should be linked to only one item. This list doesn't show me the items that are linked. And that is just the thing I would like to know.
Can someone help me out?
Update
Below is a data sample. As you can see type_id 5 and 6 are linked multiple times. What I would like to get as a result is only these records.
| id | item_id | type_id | cat |
+-------+-----------+-----------+-----------+
| 1 | 100 | 5 | cat1 |
| 2 | 110 | 5 | cat1 |
| 3 | 115 | 6 | cat1 |
| 4 | 120 | 7 | cat1 |
| 5 | 125 | 5 | cat1 |
| 6 | 130 | 6 | cat1 |
| 7 | 135 | 4 | cat1 |
| 8 | 140 | 8 | cat1 |
You need to join your itemTypes table to the query you currently have, rephrased as a subquery:
SELECT t1.*
FROM itemTypes t1
INNER JOIN
(
SELECT item_id
FROM itemTypes
WHERE category = 'cat1'
GROUP BY item_id
HAVING COUNT(*) > 1
) t2
ON t1.item_id = t2.item_id;
The logical problem with your current query is that it can only find item_id values which meet your criteria, but not the other column values.
I have the following query:
SELECT
items.*
FROM
`items`
INNER JOIN
`users` ON `items`.`owner` = `users`.`id`
GROUP BY
`items`.`owner`
LIMIT
10
I ensures it is grouped by the user (only one item fetched per user), but I also wish ensure that items with the category, say, "1" only appears once.
But that does not work. Well, query succeeds, but it does not group by category. Multiple categories is still shown. Any ideas?
I have created a SQLFiddle here: http://sqlfiddle.com/#!2/0a4bad/1
Instead of outputting:
+----+----------+-------+
| ID | CATEGORY | OWNER |
+----+----------+-------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 4 |
| 5 | 2 | 5 |
+----+----------+-------+
It should be outputting:
+----+----------+-------+
| ID | CATEGORY | OWNER |
+----+----------+-------+
| 1 | 1 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 4 |
| 5 | 2 | 5 |
| 8 | 3 | 3 |
+----+----------+-------+
(notice category 1 is only shown ONCE).
I want to ensure that only one item per owner is shown, and then adtionally ensure that a specific category (say 1 and 5) is only shown once. The category 1 and 5 are overpopulated, and if they are not limited, they will be 90% of the output.
You can use DISTINCT to retrieve unique data:
SELECT DISTINCT items.category
select * from items t1
where category not in (1,2)
or not exists (
select 1 from items t2
where t2.id < t1.id
and t2.category = t1.category
)
group by owner
http://sqlfiddle.com/#!2/0a4bad/27
Considering the following tables in a MYSQL database:
table1:
+------+-----------+----------------------+
| id | atual | category_id | user |
+------+-----------+--------------|-------+
| 1 | 100 | 1 | 1 |
| 2 | 150 | 2 | 1 |
| 3 | 50 | 1 | 2 |
+------+-----------+--------------|-------+
table2:
+------+-----------+----------------------+
| id | budget | category_id | user |
+------+-----------+--------------|-------+
| 1 | 100 | 2 | 1 |
| 2 | 150 | 1 | 2 |
| 3 | 50 | 1 | 1 |
+------+-----------+--------------|-------+
table3:
+------+-----------+
| id | name |
+------+-----------+
| 1 | one |
| 2 | two |
| 3 | three |
+------+-----------+
I want to calculate the totals for 'atual' and 'budget' given in tables 1 and 2 for a given user (1 in my example), organized by category name:
I tried the following query, which is giving me the totals for atual and budget regardless of the categories:
SELECT table2.id, table3.name AS name_category, SUM( budget ) ,
(SELECT SUM( atual) FROM table1 WHERE user =1)
FROM table2 INNER JOIN table3
ON table2.category_id=table3.id
Here is a method:
select t3.id, t3.name, sum(actual) as actual, sum(budget) as budget
from ((select category_id, sum(actual) as actual, NULL as budget
from table1
where user = 1
group by category_id
) union all
(select category_id, NULL as actual, sum(budget) as budget
from table2
where user = 1
group by category_id
)
) ab join
table3 t3
on ab.category_id = t3.id
group by t3.id, t3.name;
I need help with query
let say i have 2 tables
tableA
------------------------------------------
id | name
1 | ABC
2 | DEF
3 | GHI
tableB
------------------------------------------
id | anotherID | Amount
1 | 1 | 1000
2 | 1 | 2000
3 | 1 | 3000
1 | 2 | 4000
2 | 2 | 5000
1 | 3 | 6000
i need this list
jointable
------------------------------------------
id | anotherID | Amount
1 | 1 | 1000
2 | 1 | 2000
3 | 1 | 3000
1 | 2 | 4000
2 | 2 | 5000
3 | 2 | 0
1 | 3 | 6000
2 | 3 | 0
3 | 3 | 0
i need every single id have value even its 0 for every single anotherID value..
example :
tableA have 3 id & distinct anotherID tableB have 3 records so i will need 3 * 3 = 9 records..
how to do this with query?? i can do with left join and union but i want to know if there some more effective way to do it.
SQLFiddle demo
select T1.ID,T2.AnotherId,
COALESCE(T3.Amount,0) as Amount
FROM TableA as T1
CROSS JOIN (Select distinct anotherID FROM TableB) as T2
LEFT JOIN TableB as T3 on (T1.id=T3.id)
AND
(T2.anotherID=T3.anotherID)
Order by T2.anotherId,T1.Id
I have the following table structure:
item_id | value |
==================
1 | 1 |
1 | 3 |
1 | 4 |
2 | 2 |
2 | 3 |
2 | 4 |
2 | 5 |
3 | 1 |
3 | 5 |
3 | 6 |
4 | 1 |
4 | 3 |
4 | 4 |
4 | 5 |
I have a query that returns those item_id whose value matches with 1, 3 and 4.
So here, the item_ids that should be returned are 1 and 4.
My query:
select item_id from table t
where exists (select item_id from table t1 where value = 1 and t1.item_id = t.item_id)
and exists (select item_id from table t1 where value = 2 and t1.item_id = t.item_id) group by item_id
This query is working fine. Here i am matching only 3 values. What if i want to match 50 such values from the table? (all the 50 values are stored in a php array) The query will be huge and also i want to do the same thing from two different tables in the same query. So, this will double the size of an already huge query. Please suggest me some other way around.
Edited::
table 2
--------
item_id | user_id |
==================
1 | 1 |
1 | 5 |
1 | 7 |
2 | 2 |
2 | 3 |
2 | 4 |
2 | 5 |
3 | 1 |
3 | 5 |
3 | 6 |
4 | 1 |
4 | 3 |
4 | 4 |
4 | 5 |
Now, i want item_id where values from table1 are 1,3,4 and user_id from table2 are 1,5,7
This problem is called Relational Division.
SELECT item_ID
FROM tableName
WHERE value IN (1,3,4)
GROUP BY item_ID
HAVING COUNT(*) = 3
if uniqueness was not enforce on column value for every item_id, DISTINCT is required to count only unique values,
SELECT item_ID
FROM tableName
WHERE value IN (1,3,4)
GROUP BY item_ID
HAVING COUNT(DISTINCT value) = 3
SQLFiddle Demo (both query included)
SQL of Relational Division