Column count in a SQL Query - mysql

I want to put COUNT(item_id) in this statement:
SELECT * FROM `notifications` WHERE `uid` = '3' AND `seen` = '0' AND id IN (
SELECT MAX(id), COUNT(item_id)
FROM `notifications`
GROUP BY item_id
) ORDER BY id DESC
But This error occurred: Operand should contain 1 column(s).
Table:
[id] [uid] [item_id] [seen]
1 3 69 0
2 3 69 0
3 3 70 0
4 3 69 0
5 3 70 0
6 3 69 0
Expected output: (Order BY id DESC) where 69 is the last record.
[item_id] [num]
69 4
70 2

Given your sample data and expected results, there's no need for a subquery:
select item_id, count(*)
from notifications
group by item_id
where uid = 3 and seen = 0
order by max(id) desc;
Sample Demo

An educated guess says that you want a JOIN:
SELECT n.*, nmax.cnt
FROM notifications n JOIN
(SELECT item_id, MAX(id) as max_id, COUNT(item_id) as cnt
FROM notifications
GROUP BY item_id
) nmax
ON n.item_id = nmax.item_id AND nmax.id = nmax.max_id
WHERE n.uid = 3 AND n.seen = 0 -- removed the single quotes because these are probably numbers
ORDER BY n.id DESC;
It is unclear whether you want the filtering conditions in the subquery as well.

Related

just show not repeated value

This is my table
id
c_id
number
3444
34
3377752
3446
35
3473747
3447
35
3532061
3454
37
3379243
3455
38
3464467
3456
38
3377493
I want to create a table which is show me not repeated value in a c_id column (repeat just 1 time). the result should be:
id
c_id
number
3444
34
3377752
3454
37
3379243
GROUP BY c_id, number
HAVING COUNT(cart_id) = 1
i tried this but it shows me repeated value again
SELECT ANY_VALUE(id) id,
c_id,
ANY_VALUE(`number`) `number`
FROM tablename
GROUP BY c_id
HAVING COUNT(*) = 1;
You can do it as follows :
select t.*
from _table t
inner join (
select c_id
from _table
group by c_id
having count(1) = 1
) as s on s.c_id = t.c_id;
Check it here : https://dbfiddle.uk/RlxxQ_05

Get records group by multiple column mysql

Here is my table structure
comment_file_id comment_id attachement_id source created_by
33 203 42 IN 101
35 203 46 OUT 101
36 203 42 OUT 101
37 203 42 OUT 101
i want to get only 3 records only as records associated with attachment_id 42 are duplicates for source OUT , so oi have to exclude those ones.
select *
from comments
where comment_id = 203
GROUP
BY source
, created_by
, attachement_id ;
but getting errors
use min()
select min(comment_file_id) as comment_file_id,comment_id,attachement_id,source
,created_by from table
group by comment_id,attachement_id,source
,created_by
If you are using MySQL 8+, then ROW_NUMBER is helpful here. We can phrase your query as a union of all records not subject to duplicate constraints, along with the single record not deemed a duplicate:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY comment_file_id) rn
FROM comments
WHERE comment_id = 203 AND attachement_id = 42 AND source = 'OUT'
)
SELECT comment_file_id, comment_id, attachement_id, source, created_by
FROM comments
WHERE comment_id = 203 AND (attachement_id <> 42 OR source <> 'OUT')
UNION ALL
SELECT comment_file_id, comment_id, attachement_id, source, created_by
FROM cte
WHERE rn = 1;

MYSQL - having count(*) without group by

I have a MySQL table
discount_vouchers
------------------
id
email
test_id
My goal is to list all vouchers that appears more than once with a given email and a given test_id from the GROUP BY:
GROUP BY email, test_id
HAVING count(*) >1
How to get read of this group by?
Here is an example:
discount_vouchers
------------------
1 1#test.com 20
2 1#test.com 10
3 1#test.com 20
4 2#test.com 30
I would like to have as a result:
id email test_id count
1 1#test.com 20 2
2 1#test.com 10 1
3 1#test.com 20 2
4 2#test.com 30 2
Try something like the following
SELECT C2, counter from
(SELECT C2, COUNT(*) as counter FROM test.mytable
GROUP BY C2) as aggregation
WHERE counter > 1
Without using group by, you can do something like
SELECT a.* ,
(SELECT count(*) FROM discount_vouchers b
WHERE a.email = b.email AND a.test_id = b.test_id) as count
FROM discount_vouchers a
How about this?
Aggregate using a subquery, and use its results in order to enrich the actual table:
SELECT `discount_vouchers`.*, `counts`.`count`
FROM `discount_vouchers`
INNER JOIN (SELECT `email`, `test_id`, Count(*) AS 'count'
FROM `discount_vouchers`) AS `counts`
ON `discount_vouchers`.`email` = `counts`.`email`
AND `discount_vouchers`.`test_id` = `counts`.`test_id`;

need to filter duplicate record from a particular selected records

need to filter duplicate record from a particular selected records
SQL table site_metatag and i am using MySQL client version: 5.5.30
metatag_id | store_id | name
1 0 copyright
2 0 author
3 0 robots
4 0 googlebot
5 0 revisit-after
6 0 google-site-verification
9 1 google-site-verification
8 1 revisit-after
10 1 googlebot
11 1 robots
12 2 googlebot
13 2 robots
14 2 google-site-verification
need those record from store_id in 1 and 0 but name will be unquie like
distinct() function give uniqe all record , but i need record that not used more then 1 time
metatag_id | store_id | name
1 0 copyright
2 0 author
3 0 robots
i try
SELECT * FROM site_metatag v
where metatag_id NOT IN
(
select metatag_id from site_metatag p where v.name=p.name
)
AND v.store_id in (0,1)
but not working..
I think you want this:
SELECT
metatag_id, store_id, `name`
FROM
site_metatag v
group by `name`
Or this, if you want to group by number of occurrences(in this case 1 -> cnt = 1).
SELECT
metatag_id, store_id, v.`name`, a.cnt
FROM
site_metatag v
inner join
(select
`name`, count(*) as cnt
from
site_metatag
group by `name`
having cnt = 1) as a ON (v.`name` = a.`name`)
group by v.`name`
use distinct() function. see Distinct MYSQL
what about this:
SELECT * FROM site_metatag WHERE v.store_id in (0,1) GROUP BY name

How to add value to a subquery?

I have this simple query:
SELECT DISTINCT images.id
FROM `images`
WHERE `images`.`user_id` IN
(SELECT following_id
FROM `follows`
WHERE `follows`.`follower_id` = 5717
AND `follows`.`accepted` = 1)
ORDER BY id DESC LIMIT 1000
OFFSET 0
I want to append a value to the list of values returned by the subquery. So if the subquery returns:
23
59
14
77
I want it to be:
23
59
14
77
86
(note the extra 86 at the bottom).
If you always want 86 to be added, then just use a UNION ALL on the subquery, this will ensure that 86 is always included in your IN clause subquery:
SELECT DISTINCT spentits.id
FROM `spentits`
WHERE `spentits`.`user_id` IN
(SELECT following_id
FROM `follows`
WHERE `follows`.`follower_id` = 5717
AND `follows`.`accepted` = 1
UNION ALL
SELECT 86)
ORDER BY id DESC LIMIT 1000
OFFSET 0
You could also just add it as a 2nd piece of criteria in your WHERE clause:
SELECT DISTINCT spentits.id
FROM `spentits`
WHERE `spentits`.`user_id` IN
(SELECT following_id
FROM `follows`
WHERE `follows`.`follower_id` = 5717
AND `follows`.`accepted` = 1)
OR `spentits`.`user_id` = 86
ORDER BY id DESC LIMIT 1000
OFFSET 0