SELECT T2.ITEM_ID AS item_ITEM_ID
FROM types T
JOIN items T2
ON T.ITEM_ID = T2.ITEM_PARENT_ID
WHERE T.ITEM_TYPE = 'I'
AND T2.ITEM_TYPE = 'I'
This query is returning rows from T2 that have duplicate T2.ITEM_PARENT_IDs. How would I specify this query to return only rows that with unique T2.ITEM_PARENT_IDs ?
Count the repeats and filter out when they're more than 1.
SELECT T2.ITEM_ID AS item_ITEM_ID, COUNT(*) dups
FROM types T
JOIN items T2
ON T.ITEM_ID = T2.ITEM_PARENT_ID
WHERE T.ITEM_TYPE = 'I'
AND T2.ITEM_TYPE = 'I'
GROUP BY item_ITEM_ID
HAVING dups = 1
DEMO
Related
I have the following query:
SELECT c.text1, sum(c.num1), sum(c.num2), sum(c.num3),
(SELECT count(id) FROM table2 WHERE type = 1 AND txt = c.text1 AND spec_id = c.sp_id)
FROM table1 as c
WHERE c.type = 1
GROUP BY c.text1, c.sp_id
Is there a workaround to loose the c.sp_id from the GroupBy clause somehow? I know that if I remove it MySQL will return an error.
Or is there a way to group the results of this query by c.text1 only?
If I understand the problem correctly, you need to do two separate aggregations. This is one version of the query:
SELECT c.text1, c.sum1, c.sum2, c.sum3, t2.cnt
FROM (SELECT c.text1, sum(c.num1) as sum1, sum(c.num2) as cum2, sum(c.num3) as sum3
FROM table1 c
GROUP BY c.text1
) c LEFT JOIN
(SELECT txt, count(*) as cnt
FROM table2 t2
WHERE t2.type = 1 AND
EXISTS (SELECT 1
FROM table1 c2
WHERE t.txt = c2.txt AND c2.type = 1 AND
t.spec_id = c2.sp_id
)
) t2
ON t2.txt = c.text1
WHERE c.type = 1;
I am trying to update the table T1 by counting the results of a SQL Query from other tables T2 and T3.
This is the query I came up with but it does not seem to work:
UPDATE T1
set Stock =
(SELECT count(ID_Item)
FROM T2,T3
WHERE T2.ID_Product = T1.ParentSKU AND
T3.ID = T2.ID_Product AND
Stock_Items.Name = '' AND
Stock_Items.Returned = ''
GROUP BY(T3.Size)
)
What I am trying to do is to update T1 by counting the results from T2 and grouping distinct sizes from T3.
You need to remove the GROUP BY, if your item has more than one size your subquery will return multiple results, and the update will fail.
UPDATE T1
set Stock =
(SELECT count(ID_Item)
FROM T2 INNER JOIN T3
WHERE T2.ID_Product = T1.ParentSKU AND
T3.ID = T2.ID_Product AND
Stock_Items.Name = '' AND
Stock_Items.Returned = '' AND
T3.Size = T1.Size
)
if Size is not a factor, then remove it completely from the subquery
I'm trying to inner join table1 <- to -> table2, table3, table4 OR table5 depending on a field present in table1. So, if the field information in table1 has the value example_get_from_two I should inner join table1 with table2 as to obtain a given row, if in another row table1 has the value example_get_from_three I should inner join table1 with table3 and so on. This is the query I tried, but it returned zero rows:
SELECT n.notification_type,
(CASE WHEN n.notification_type = 'two' AND n.information = t2.somefield
THEN t2.anotherfield
WHEN n.notification_type = 'three'
THEN (CASE WHEN t3.field = '1' THEN t3.otherfield ELSE t3.yetanotherfield END)
WHEN n.notification_type = 'four' AND t4.field = n.information
THEN t4.anotherfield
WHEN n.notification_type = 'five'
THEN t5.field
END) AS information FROM table0 zero, notifications n
INNER JOIN table1 t1 ON(n.information=t1.somefield AND n.notification_type = 'something')
INNER JOIN table2 t2 ON(n.information=t2.somefield AND n.notification_type = 'something')
INNER JOIN table3 t3 ON((n.information=t3.somefield OR n.information=t3.someotherfield) AND n.notification_type = 'something')
WHERE zero.field ='something' AND n.id = zero.id
Unfortunately this retrieves zero rows and shouldn't , probably because all joins are made despite the actual value of information, and n.notification_type = "something". Is this possible with cases or similar? What I'd like to obtain is
fromfield1 | fromfield2 | fromfield3 (this one is "dynamic" and
depends on the joined table)
I have been using the following SQL:
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event
WHERE t2.Event IS NULL
To select all rows from table 1 where table 2 is Null. This effectively filters out all my Table 1 data where Table 2 has data. However, I want to apply this only when a column in table 2 equals a certain value. Therefore I am looking to do a SELECT * FROM t2 WHERE t2.ID = 1 but am unsure how this fits into this query.
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event and t2.certain_column = 1234
WHERE t2.Event IS NULL
Also you can try query with NOT EXISTS:
SELECT DISTINCT NAME
FROM Events t1
WHERE NOT EXISTS(SELECT * FROM UserHistory t2
WHERE t1.Name = t2.Event AND t2.ID = 1)
You need to add the predicate to the JOIN condition:
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event AND t2.ID = 1
WHERE t2.Event IS NULL;
If you add it to the WHERE you effectively turn your outer join into an inner join, meaning no rows will be returned (since NULL = 1 evaluates to false)
SELECT
count(t1.id) AS c1
FROM
table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c1 = 6 -> CORRECT!
SELECT
count(t2.id) AS c2
FROM
table2
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c2 = 1 -> CORRECT!
SELECT
count(t1.id) AS c1,
count(t2.id) AS c2
FROM
table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE
table2.mode = 'ls'
GROUP BY
t1.id
c1 = 6 -> CORRECT!
c2 = 6 -> WRONG!
How do I request both counts in one query, without getting wrong results?
I need to count two different requests at the same table (table1).
so, I'm using an alias for both request. (t1). Each alias-request is working fine alone. If I use both in the same query, i got wrong results.
count() will get you the number of records that are returned by your query. Since if you removed the counts and replaced it with * you would have 6 rows both of those counts are giving you 6.
Is there any reason why you cant use two sub selects and return the result of each of those?
So:
SELECT subQ1.c1, subQ2.c2 FROM
(SELECT count(t1.id) AS c1 FROM table2
LEFT JOIN table1 AS t1 ON (t1.uid = table2.uid)
WHERE table2.mode = 'ls') as subQ1,
(SELECT count(t2.id) AS c2 FROM table2
LEFT JOIN table1 AS t2 ON (t2.pid = table2.id)
WHERE table2.mode = 'ls') as SubQ2;
I believe your problem on the full query is your group by function. You are grouping by t.id, thus a1.id will have a different count based on how many rows you have.
What I mean by this is if there are 6 rows in table t, then count is going to return 6 for table t; but also since there looks to be a 1 to 1 relation on table a, there are 6 matching rows in table a to the 6 matching rows in table t. such that
t.id = a.id
1 = 1
2= 2 ...etc.
Thus your count is returning rows versus the count you believe you should have? I believe sum function is what you want to use here.
You could try this...but I'm not really sure what you're trying to do.
SELECT (...)
count(CASE WHEN t1.uid = t3.uid THEN t1.id ELSE NULL END) AS CBanz,
count(CASE WHEN ta1.pid = t3.id THEN a1.id ELSE NULL END) AS CBanz1
FROM
t0
LEFT JOIN (...)
LEFT JOIN t1 ON (t1.uid = t3.uid)
LEFT JOIN t1 AS a1 ON (a1.pid = t3.id)
WHERE (...)