lets say
My table contains
Id Status
1 0
2 1
3 0
4 0
5 1
6 0
I need output like
Id Status
5 1
I tried like Max(id) but it gives output as
id status
6 0
I can only suppose, that you want to know about the maximum id of those entries having Status=1, right? Then use
select max(id) from mytable where Status=1
Try this:
SELECT id, status
FROM myTable ORDER BY `id` DESC LIMIT 1 , 1
I have assumed that you are looking for the second highest Id record values.
I guess you would like to have the max(id) where Status=1?
Then use select max(id) from table where Status=1
Since you want to get the absolute max from both columns use :
SELECT GREATEST(MAX(id), MAX(status));
Your question is a bit unclear what it is you actually want. From your example, I guess you want the maximum ID for the max status.
This is the max id for every status:
select max(id), status from table
group by status;
This will result in 5,1 and 6,0.
You could then filter out what you don't need, e.g.
select * from (
select max(id), status from t
group by status
) maxidperstatus
where maxidperstatus.status = (select max(status) from t);
Try this also
select Id,Status
from table
where Id=(
select MAX(Id) from table where Id <> (select MAX(Id) from table)
)
I assume that you want the Id where the status is the biggest
Try this
select max(status),id from table group by id order by max(status) DESC
Related
so, i have this table generated annually :
+----+------+
| id | name |
+----+------+
| 1 | 20162|
| 2 | 20162|
| 3 | 20171|
| 4 | 20171|<<<||| "how do i get this bfore max value"
| 5 | 20172|
| 6 | 20172|
+----+------+
If i query :
SELECT name FROM table WHERE where name=(SELECT max(name))
The result is 20172
How do i get the value before that (20171)?
This will get second max.
SELECT *
FROM table
WHERE name NOT IN (SELECT MAX(name) FROM table )
ORDER BY id DESC LIMIT 1
You can do the little trick:
SELECT name FROM table ORDER BY name DESC LIMIT 1,1
If you want a portable solution (since the above will only work in MySQL probably):
SELECT name FROM table t1 where
1 = (SELECT count(distinct name)
from table t2
where t2.name > t1.name )
This selects the name which has exactly one other name which is greater than it. It will have issues when e.g. all values are the same but that may actually be the intention sometimes.
Also you can try this:
SELECT name
FROM (SELECT DISTINCT name FROM yourtable) TMP
ORDER BY name DESC LIMIT 1,1
how about this:
select b.second_id,b.name from
(select id, max(name) as max from tbl_max) as a
LEFT JOIN
(select id as second_id, name from tbl_max ) as b
on a.id <> second_id where name < a.max order by name desc limit 1;
Another solution much better:
set #max= 0;
select max(name) into #max from tbl_max;
select * from tbl_max where name < #max ORDER BY id desc limit 1
it return a result of
4 20171
If you want Max(name)
SELECT id,MAX(name) FROM table
WHERE name < (SELECT MAX(name) FROM table )
or
SELECT id,MAX(name) FROM table
WHERE name <> (SELECT MAX(name) FROM table )
And there is an article for Find nth high value
(as Question changed later, this tricks is not useful for this problem)
can you use this simple order by ?
SELECT * FROM tablename
group by name
order by name desc
limit 1,1
if you have multiple equal values in MAX(name) you need group by, even this query is not general solution!
You can try below code.It will give you nth highest record.
SELECT NAME FROM table ORDER BY name DESC LIMIT n,1
Hope this will helps
SELECT DISTINCT name FROM table ORDER BY name DESC LIMIT 1,1;
The LIMIT clause accepts two values, an offset and a count. When only 1 value is provided, it assumes the offset is 0
DISTINCT is necessary as you have duplicate values for name. If this was unintentional, it's not needed.
Example at http://sqlfiddle.com/#!9/ea6e2/2
i have a sample data like this:
Now i want to get lastest data used to have user_id = 41 and highest price, output like this:
How can i do it with SQL command line ?
Thank for read
Try this query
select User_id,auction_id,price from tablename where price in(select price from tablename where id in(select max(id) from tablename group by user_id))
As per your output that you wish try this:
select user_id,auction_id,price
from table_name
where user_id=41
order by id desc;
If you have updated output incorrectly then try it as per your provided conditions-
select user_id,auction_id,price
from mytable
where user_id=41
order by auction_id;
But if as per your output you want 1 latest row of user_id=41 and then all rows of highest price then you can try it-
SELECT user_id,auction_id,price
FROM mytable
WHERE user_id=41
ORDER BY id DESC LIMIT 1
UNION
SELECT b.user_id,b.auction_id,price
FROM mytable b
JOIN (
SELECT MAX(price) AS price
FROM mytable
) a ON a.price=b.price;
I have a database with one table called "user" having two fields:
"id" (type: INTEGER, PRIMARY KEY)
"name" (type: VARCHAR(32))
I want to Write a standard SQL query which retrieves the second highest value of "id" from the "user" table. The value returned should be represented using the column name "id".
I have tried this but it gives me all ids:
SELECT `user`.`id`
FROM `user`
ORDER BY `user`.`id` ASC
LIMIT 0 , 30
some example data in my table:
id name
----------------
1 john
2 david
3 mike
I want to get '2' but now i'm getting :
id
----
1
2
3
I can do it with help of PHP but I want to know the way with mysql (SQL).
thanks
SELECT id
FROM `user`
ORDER BY id DESC -- start with highest
LIMIT 1 -- show only 1 row
OFFSET 1 ; -- but skip the first (skip 1 row)
SELECT id FROM user ORDER BY id ASC LIMIT 1, 1
select max(id) from user where id < (select max(id) from user);
select max(id) from user where id not in (Select max(id) from user);
SELECT id
FROM `user`
ORDER BY id ASC
LIMIT 1, 1
Using limit you can set an offset and the number of records you like being returned.
How 'bout
Select max(id) from user
where id <> (select max(id) from user)
This may work:
select max(id)-1 from user;
I have a table :
ID | time
1 | 300
1 | 100
1 | 200
2 | 200
2 | 500
I want to get 2nd row for every ID
I know that I can get 1st row as
select ID,time from T group by ID;
But I don't know about how to get 2nd row for every ID.
I know about limit and offset clause in mysql, but can't figure out how to use them here.
How can I do it ?
EDIT : Actually, time is not ordered. I forgot to specify that. I have made an edit in the table.
i have just an idee how to make it but i couldnt fix it , maybe you can fix it. any suggest is appreciated to correct my query
first this to select the first row of each id.
SELECT min(id) id
FROM TableName t2
group by id
then select the min(id) which are not in the first query to select to min(id) (which is second row)
like that
SELECT min(id) id ,time
FROM TableName
WHERE id NOT IN (
SELECT min(id) id
FROM TableName
GROUP BY id
)
GROUP BY id
** as i said its just suggest . it returns me 0 values.if u fix it let me edit my post to be helpful
here a demo
SELECT ID, MAX(time) time
FROM
(
select ID, Time
from TableName a
where
(
select count(*)
from TableName as f
where f.ID = a.ID and f.time <= a.time
) <= 2
) s
GROUP BY ID
SQLFiddle Demo
SELECT x.*
FROM test x
JOIN test y
ON y.id = x.id
AND y.time >= x.time
GROUP
BY id,time
HAVING COUNT(*) = n;
Note that any entries with less than n results will be omitted
You cannot do this with the tables that you have. You could make a valiant attempt with:
select id, time
from (select id, time
from t
group by t
) t
where not exists (select 1 from t t2 where t2.id = t.id and t2.time = t.time)
group by id
That is, attempt to filter out the first row.
The reason this is not possible is because tables are inherently unordered, so there is not real definition of "second" in your tables. This gives the SQL engine the opportunity to rearrange the rows as it sees fit during processing -- which can result in great performance gains.
Even the construct that you are using:
select id, time
from t
group by id
is not guaranteed to return time from the first row. This is a (mis)feature of MySQL called Hidden Columns. It is really only intended for the case where all the values are the same. I will admit that in practice it seems to get the value from the first row, but you cannot guarantee that.
Probably your best solution is to select the data into a new table that has an auto-incrementing column:
create table newtable (
autoid int auto_increment,
id int,
time int
);
insert into newtable(id, time)
select id, time from t;
In practice, this will probably keep the same order as the original table, and you can then use the autoid to get the second row. I want to emphasize, though, the "in practice". There is no guarantee that the values are in the correct order, but they probably will be.
I have this mysql query which works in getting the specific data by specified date;
SELECT id FROM mytable WHERE DAY(idate)='11' AND MONTH(idate)='01' AND YEAR(idate)='2013' GROUP BY id
now i want to get the data from the date specified and the data outside the date specified, I've tried this query and returns zero results;
SELECT id FROM mytable WHERE DAY(idate)='11' AND DAY(idate)<>'11' AND MONTH(idate)='01' AND YEAR(idate)='2013' GROUP BY id
Your SQL:
SELECT id FROM mytable WHERE DAY(idate)='11' AND DAY(idate)<>'11'
AND MONTH(idate)='01' AND YEAR(idate)='2013' GROUP BY id
is saying "Select the id for the rows from mytable where the day is both equal to 11 and also not equal to 11..."
This is an impossible situation, hence you're getting nothing.
What you actually want is "Select the id for the rows from mytable where the day is equal to 11 as well as the rows where date is not equal to 11...", I think. This is the same as "Select the id for the rows from mytable regardless of the value of the date..." or:
SELECT id FROM mytable WHERE MONTH(idate)='01' AND YEAR(idate)='2013' GROUP BY id
Try OR instead of AND:
SELECT id
FROM mytable t1
WHERE DAY(idate)<>'11'
OR ( DAY(idate) = '11'
AND MONTH(idate)='01'
AND YEAR(idate)='2013'
)
GROUP BY id
you can use your first query and subtract from the queried table, or you can fix your query (read about De-Morgan's laws):
SELECT id
FROM mytable
WHERE DAY(idate)<>'11' OR MONTH(idate)<>'01' OR MONTH(idate)='01' OR YEAR(idate)='2013'
GROUP BY id
Your query at the moment basically has this as a WHERE clause:
where 1 = 1
and 1 <> 1
Obviously both cannot be true. Similarly your filters on DAY(idate) are mutually exclusive. It is difficult to understand exactly what you're trying to achieve. Perhaps you should jyst remove the tests on DAY() altogether?
SELECT id
FROM mytable
WHERE MONTH(idate)='01'
AND YEAR(idate)='2013'
GROUP BY id