equal and not equal - mysql

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

Related

how to find next max(id) in a table

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

Select last value in SQL table with conditions

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;

UNION two ordered MySQL statements

I have a table with 3 columns (id, name, code) and 10 rows. Some of the rows don't have a code so that column is empty for some. What I'm trying to accomplish is SELECT the rows with code column not empty first ordered by last inserted followed by all rows with code column empty ordered by last inserted.
I have tried
(SELECT * from tablename WHERE code <> '' ORDER BY ID DESC) UNION
(SELECT * from tablename WHERE code = '' ORDER BY ID DESC)
The UNION works but the order does not. I have read here about other questions and found out adding ORDER BY like I added will not work and I should add it at the end but that would not help me accomplish what I want and will mix rows that have a code with rows that don't.
Is there a way to succeed with what I'm looking for?
I think you just need to put your sort logic in the ORDER BY clause
SELECT id, name, code
FROM tablename
ORDER BY code = '', ID desc;
Try this:
SELECT * FROM
(
(SELECT * from tablename WHERE code <> '' ORDER BY ID DESC)
UNION
(SELECT * from tablename WHERE code = '' ORDER BY ID DESC)
)tab ORDER BY ID DESC;
Or
SELECT * from tablename ORDER BY code DESC,ID DESC
Change ASC/DESC as per you want it to show

select 2nd row of every ID in mysql

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.

Fastest query to see if it returns at least one row

I just need to know if a query returns or not a record.
Of course I can do this:
SELECT COUNT(*) FROM tbl WHERE conds;
But this returns the exact number of rows (of course), and I don't need this overhead.
So I thought this query:
SELECT COUNT(*) FROM (SELECT id FROM tbl WHERE conds LIMIT 1) as t1
Limiting the internal query to 1.
Is this faster? Or considering I am doing a subquery it cancels the benefits of LIMIT 1?
Note: for everyone asking theirself, I can't apply LIMIT 1 to the first query because it doens't work
The inner-select in the second query is redundant.
If you just want to check at-least of one row :-
SELECT 1 FROM tbl // return 1
WHERE conds // depends on your index and query
ORDER BY NULL // avoid file-sort
LIMIT 1; // minimum row
Why not just:
SELECT 1 FROM tbl WHERE conds LIMIT 1
You could do:
SELECT 1 WHERE EXISTS(SELECT id FROM tbl WHERE CONDITION)
Or something like:
SELECT 1 WHERE EXISTS (SELECT id FROM tbl WHERE id IN( 1000, 1001))