How to select next 4 consecutive rows from my resultant row? - mysql

I want to know, how can i select next 4 consecutive rows along with the resultant row in MySQL?
To explain:-
i have following rows in table abc
name | amount
a - 1000
b - 2000
c - 3000
d - 4000
e - 5000
f - 6000
Now,
if my selection criteria is name = a , then resultant rows returned should be
a - 1000
b - 2000
c - 3000
d - 4000
e - 5000
In other words i want a query which will return result of query - "select * from abc where name = 'a'" plus next 4 consecutive rows to that row?

SELECT abc.*
FROM abc JOIN (SELECT amount FROM abc WHERE name = 'a') t
WHERE abc.amount >= t.amount
ORDER BY abc.amount
LIMIT 5

try
for mssql: select top 5 * from abc where name > 'a' order by name
for mysql: select * from abc where name > 'a' order by name limit 5

Related

Mysql results differs at evens from singles in same query

I have a strange error first time seing such results maybe someone else also found how to overpass such odd error.
I have a table structure like this :
|id|uid|test1|test2|rid|
1 1 - - 1
2 1 - - 1
3 2 - - 2
4 2 - - 2
5 3 - - 3
6 3 - - 3
7 4 - - 4
8 4 - - 4
----------------------------
as "-" are varchar data all other columns are integers
My query with the odd results is this :
SELECT COUNT(uid) AS COUNT,
id AS ID,
uid AS InqID,
test1 AS A,
test2 AS B
FROM test_table
WHERE rid = (X)
GROUP BY uid
ORDER BY id DESC
As results i get
if (X) = 1 or 3 it shows me the correct last id number (2 id for 1 and 6 id for 3)
but if (X) is 2 or 4 it shows me the first in row id number (3 id for 2 instead of 4 and 7 id for 4 instead of 8)
Can anyone tell me why i get the correct results only as singles in rid and not at even numbers of rid column or how at least this query will work as it has to ?
Thank you all in advance
Use MAX(id) instead of ORDER BY.
SELECT COUNT(uid) AS COUNT, MAX(id) AS ID, uid AS InqID, test1 AS A, test2 AS B
FROM test_table
WHERE rid = (X)
GROUP BY uid

Joining a table with itself vs using Group By MySQL

Here is a simplified version of my table:
It contains the columns
employee_id column2 column3 x y
and they are all NUMERIC
I am having trouble writing a complicated MySQL query. I want to get all the employeeid and column3 values for all employees that have the same value in column2 as another employee and have DIFFERENT values for x and y PAIR from every other employee. For example, if the following 4 rows were in table:
2 100 123.456 5 7
1 234 123.456 5 7
3 100 456.789 5 10
4 100 123.456 5 7
The rows 2 100 123.456 5 7 and 3 100 456.789 5 10 should be obtained because they have different employee ids (2 vs 3), the same value for column2 (100 and 100), and different x, y pair: (Employee 2 has x = 5 and y = 7, which is distinct from x = 5 and y = 10).
How can I compare the documents of table with other documents within itself?
SELECT e1.*
FROM employee e1
JOIN employee e2
ON e1.employee_id <> e2.employee_id
AND e1.column2 = e2.column2
AND e1.X <> e2.X
AND e1.Y <> e2.Y
But I guess your last condition should be
AND (e1.X <> e2.X OR e1.Y <> e2.Y)

How to select only last rows that satisfies conditions?

I'm trying to select rows where x=5, but x changes constantly. So I have such a table:
id x
---- ---
1 5
2 6
3 4
4 5
5 5
So I want to perform a query like "SELECT * FROM table WHERE x=5 AND _???_;" so that it returns rows 4 & 5 but not row 1.
In other words, I want to get the rows where x had this value most recently. I hope I made myself clear. Thanks!
edit:
Number of entries after x got the last value my change. I mean the table could also be like this:
id x
---- ---
1 5
2 6
3 4
4 5
5 1
6 5
7 5
... 5
100 5
101 5
in this case it should return rows [6-101].
Following wil get recent row
SELECT * FROM table WHERE x=5 ORDER BY id DESC LIMIT 0,1
SQLFiddle demo
select * from t t1
where
x=(select x from t order by id desc limit 1)
and
not exists
(select x from t where id>t1.id and x<>t1.x)
or
SQLFiddle demo
select * from t t1
where
x=(select x from t order by id desc limit 1)
and
id>=
(select max(id) from t
where x<>
(select x from t order by id desc limit 1)
)
Select what is faster on your base.

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.

Select DISTINCT values from Two Columns using an inner join on the same table in MySQL

I have a query that selects 20 rows from a table, loops, and pulls a single row from the same table that falls in the desired score range. The found row is then deleted so that it will not be selected again.
user_id is unique and some rows have col1=0 and others have col1=1, therefore the second query will never select a row from the first query.
The temp table looks like this:
user_id col1
-------------------
1 0
2 0
3 1
4 1
The user table looks like this:
user_id score
-----------------
1 1000
2 2000
3 3000
4 4000
$res = do_query("SELECT temp.user_id,user.score
FROM temp,user
WHERE temp.col1=0 AND temp.user_id=user.user_id LIMIT 20");
while (($row = mysql_fetch_row($res))) {
$score = $row[1];
$alt_res = do_query("SELECT temp.user_id, user.score
FROM temp,user
WHERE temp.col1=1 AND temp.user_id=user.user_id
AND user.score<$score AND user.score>$score*0.66 LIMIT 1");
$alt_row = mysql_fetch_row($alt_res)
$user_id = $alt_row[0];
do_query("DELETE FROM temp WHERE user_id=$user_id");
}
This works just fine, however I was trying to turn this into a single query, but I keep getting duplicate values, and I can't seem to weed them out.
SELECT temp.user_id,t1.user_id,t1.score FROM (
SELECT temp.user_id,user.score
FROM temp,user
WHERE temp.col1=0 AND temp.user_id=user.user_id LIMIT 20) AS t1,temp,user
WHERE temp.col1=1 AND temp.user_id=user.user_id
AND t1.score<user.score AND t1.score>user.score*0.66 GROUP BY temp.user_id
I get 20 rows with temp.user_id being unique, but duplicates with t1.user_id.
For example:
temp.user_id t1.user_id
----------------------------
1 6
2 7
3 7
4 7
5 8
and I want:
temp.user_id t1.user_id
-----------------------------
1 6
2 7
3 8
4 9
5 10
Any idea how to make it so that no user_id is repeated in either column?
Maybe you can change LIMIT 20 to LIMIT 1 on the sub-query?