just show not repeated value - mysql

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

Related

Column count in a SQL Query

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.

Filter Results in SQL

Suppose I have a table
id value
------ ---------
10 123
10 422
11 441
11 986
12 674
13 648
I need a query which will return only those id's which have 2 or more values associated with them. So, in that case it will only return ID 10 & 11, but i need al the records.
so the result looks like:
id value
------ ---------
10 123
10 422
11 441
11 986
Thank you.
select a2.*
from MyTable a2
inner join
(
select a1.id
from MyTable a1
group by a1.id
having count(*) > 1
) a3
on a3.id = a2.id
Assuming a UNIQUE KEY can be formed on (id,value)...
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.id = x.id
AND y.value <> x.value
If a UNIQUE KEY cannot be formed on (id,value), then this isn't really a table in a strict RDBMS sense.
You can use this query :
SELECT * from table where id in
( SELECT id FROM table group by id having count(id) > 1 )
With mysql 8+ or mariadb 10.2+, you would use the count window function:
select id, value
from (
select id, value, count(id) over (partition by id) as num_values
from sometable
) foo
where num_values > 1;

select only one row from multiple rows MySql

I have a orders table as follows:
id | cust_id| status
1 25 1
2 25 1
3 25 0
4 26 0
5 26 1
6 26 0
7 27 1
8 27 1
9 27 1
I need two queries:
1. fetch all the 'cust_id' having at least one status 0.
eg. in above table I should get cust_id with two rows 25 and 26 because they have at least one 0 in 'status' column.
2. fetch all the 'cust_id' having all status 1.
eg. in above table I should get cust_id with one row 27 because it has all 1's in the 'status' column.
Assuming status is numeric
select distinct cust_id
from my_table where status = 0
select disctint cust_id
from my_table
where cust_id NOT in (select distinct cust_id
from my_table where status = 0 )
these are the two queries ..
if you need both result in same select you can use union
select distinct cust_id
from my_table where status = 0
union
select disctint cust_id
from my_table
where cust_id NOT in (select distinct cust_id
from my_table where status = 0 )
SELECT DISTINCT cust_id FROM orders WHERE status=0
UNION
SELECT DISTINCT cust_id FROM orders WHERE status=1
AND cust_id NOT IN (SELECT cust_id FROM orders WHERE status<>1)

How to achieve next and previous row in a query

I have a table like this in MYSQL:
SELECT * FROM test_table
id u_id name date
1 101 name2 2012-05-14
2 305 name3 2012-05-11
3 506 name4 2012-05-05
4 207 name5 2012-05-12
5 108 name6 2012-05-03
SELECT id,u_id from test_table order by date;
id u_id
5 108
3 506
2 305
4 207
1 101
I have a application where where these things are displayed
on clicking on any u_id it takes me to a different page which displyed details stuff
Now I want to write a query which willl give me the next record
How do I achive the next record from here like
when I say u_id>305 it shoud give me 207?
and u_id<305 it should give me 506
I think this is what you are looking for:
SELECT id, u_id
FROM test_table
WHERE uid > 305
ORDER BY date ASC
LIMIT 1;
SELECT id, u_id
FROM test_table
WHERE uid < 305
ORDER BY date DESC
LIMIT 1;
Given the u_id is 305, to get the next record by date:
SELECT id,u_id
FROM test_table a
WHERE a.date > (SELECT date FROM test_table b WHERE b.u_id = 305)
ORDER BY a.date
LIMIT 1;
And to get the previous record by date:
SELECT id,u_id
FROM test_table a
WHERE a.date < (SELECT date FROM test_table b WHERE b.u_id = 305)
ORDER BY a.date DESC
LIMIT 1;
try this:
select id,u_id from
(SELECT id,u_id,#rownum:= #rownum+1 AS Sno
from test_table , (SELECT #rownum:=0) r;
order by date)a
where Sno=<#ID-1/+1>
lets say if your current Sno=5 then 4 will give the previous record and 6 will give the next record

Select distinct values from two columns

I have a table with the following structure:
itemId | direction | uid | created
133 0 17 1268497139
432 1 140 1268497423
133 0 17 1268498130
133 1 17 1268501451
I need to select distinct values for two columns - itemId and direction, so the output would be like this:
itemId | direction | uid | created
432 1 140 1268497423
133 0 17 1268498130
133 1 17 1268501451
In the original table we have two rows with the itemId - 133 and direction - 0, but we need only one of this rows with the latest created time.
Thank you for any suggestions!
Use:
SELECT t.itemid,
t.direction,
t.uid,
t.created
FROM TABLE t
JOIN (SELECT a.itemid,
MAX(a.created) AS max_created
FROM TABLE a
GROUP BY a.itemid) b ON b.itemid = t.itemid
AND b.max_created = t.created
You have to use an aggregate (IE: MAX) to get the largest created value per itemid, and join that onto an unaltered copy of the table to get the values associated with the maximum created value for each itemid.
select t1.itemid, t1.direction, t1.uid, t1.created
from (select t2.itemid, t2.direction, t2.created as maxdate
from tbl t2
group by itemid, direction) x
inner join tbl t1
on t1.itemid = x.itemid
and t1.direction = x.direction
and t1.created = x.maxdate