473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

find a specific row in a set of rows

Dormilich
8,658 Expert Mod 8TB
Hi,

I have a table where I want the position of a row in a result set that meets an additional condition.

Expand|Select|Wrap|Line Numbers
  1. +----+-------+------+
  2. | ID | valid | time |
  3. +----+-------+------+
  4. |  1 |   0   | 1:00 |
  5. |  2 |   0   | 2:00 |
  6. | 18 |   0   | 3:00 |
  7. | 24 |   0   | 3:30 |
  8. | 39 |   1   | 3:55 |  <-- need that position where `valid` = 1, i.e. "5"
  9. | 46 |   0   | 7:00 |
  10. +----+-------+------+
from the listed table above (which is already the result of a JOIN) I want the position of the marked row within the result set (the result set is already orderd by the `time` field).

I looked through the MySQL function reference, but nothing looked like it would do the job, so does anyone know how to do this?
May 11 '10 #1
5 2693
Dormilich
8,658 Expert Mod 8TB
I learned that I cannot capture all values I want when using JOINs, so I have to make sequent queries.

to get the desired value I count all items with a time or ID lower or equal to the row of interest (still untested, though).
May 12 '10 #2
chathura86
227 100+
you can use a sequence row to number the record set and get it

eg

let say you got the above result set from query X

Expand|Select|Wrap|Line Numbers
  1. select rownum from
  2.     (SELECT @rownum:=@rownum+1 rownum, tb1.*
  3.     FROM 
  4.         (SELECT @rownum:=0) r, 
  5.         (X) tb1 ) T
  6. where (T.valid > 1)
Regards
May 13 '10 #3
Dormilich
8,658 Expert Mod 8TB
could you explain that SQL or point me to an explanation of it, please? I’ve never seen such a syntax.
May 13 '10 #4
chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1. (SELECT @rownum:=0) r
initialize the @rownum (mysql variable) to 0

Expand|Select|Wrap|Line Numbers
  1. (X) tb1
create a temporary table from your sql and name it as tb1


Expand|Select|Wrap|Line Numbers
  1.  SELECT @rownum:=@rownum+1 rownum, tb1.*
  2.      FROM 
  3.          (SELECT @rownum:=0) r, 
  4.          (X) tb1
this will select all from tb1 and for each row you will have the row number
because oyu are increasing the @rownum by 1

so now you have your result set with the row number


make a temporary table from the above sql and name it as T

Expand|Select|Wrap|Line Numbers
  1. select rownum from
  2.     (SELECT @rownum:=@rownum+1 rownum, tb1.*
  3.     FROM 
  4.         (SELECT @rownum:=0) r, 
  5.         (X) tb1 ) T
  6. where (T.valid > 1)
now you can use it as a regular table for filtrations etc.

hope this will help you.

by the way did it solve your problem?

Regards
May 14 '10 #5
Dormilich
8,658 Expert Mod 8TB
by the way did it solve your problem?
I’m busy this whole weekend, so I can tell you next week at the earliest.
May 14 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
14
by: Jacko | last post by:
Hi guys, Say I made a SELECT statement to my sql DB that would return 50 rows that I will use a sqldatareader to access. Instead of iterating through each and every row of the datareader, I'd...
1
by: Ambica Jain | last post by:
I want to change the backcolor of some specific rows in the DataGrid. I have seen some samples that do it by overriding Paint method of inherited column class. However, I am using typed dataset...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
7
by: tasmontique | last post by:
I have an access table that outputs to excel using a query . However what I am trying to do is under the arrival date column specify a criteria based on the Datepart function that only displays...
0
by: james.benson1 | last post by:
Hello All, I am trying to create a DTS package. I have two tables tbl_A and tbl_B with similar data/rows but no primary keys. tbl_A is master. I would like this package to query tbl_A and...
7
by: jb1 | last post by:
Hello All, I am trying to create a DTS package. I have two tables tbl_A and tbl_B with similar data/rows but no primary keys. tbl_A is master. I would like this package to query tbl_A and...
2
by: yuval | last post by:
I need to find the rows that exist in one table but not in the other with this condition: (prod_name exist in table1 and not in table2.prod_name ) AND (prod_name exist in table1 and not in...
1
by: sejal17 | last post by:
hello everyone, I have a problem to find a specific words from url.How can we find specific words from url?pls answer me as soon as possible.......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.