473,396 Members | 2,057 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.

How to select one first occurance of record accourding to filed value

KeredDrahcir
426 256MB
I'd like to select data from a table. One column in the table (type) can have a value of 1 to 6.
I do different things with the data depending on the value. If the value comes up as 4, I need to look at a column (group) and then as I continue through the table, I need it to miss out any fields where the value of type is 4 and the value of group has come up before.

Is there a way to do this in a loop.
Expand|Select|Wrap|Line Numbers
  1.  $sql="SELECT * FROM table WHERE id=$id_value";
  2. $result=mysql_query($sql,$db) or die(mysql_error());
  3. while($row = mysql_fetch_array($result))
  4. {
  5.   $class=new class_name($db, $table, $row['id']);
  6.     process data
  7. }
Many Thanks.
Mar 15 '10 #1
5 2800
dgreenhouse
250 Expert 100+
I'm very unclear on what you're trying to accomplish here...

Could you state your problem a little better?

I understand the code you've shown, but that doesn't indicate what your issue is...
Mar 16 '10 #2
KeredDrahcir
426 256MB
When I runs to code it retrives the data from the table. I want to process the data differently depending upon the value of the type field.
If the type field gives me a value of 2, I want to look at the name field and then as I continue through the table I want any occurances of data that have type of 2, and have the name used before to be dicounted, but I want all the rest of the data to be retireved as before.
Mar 16 '10 #3
KeredDrahcir
426 256MB
I've managed to solve the problem but if anyone else is interested in the solutions, this is the code I used:
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT field1field2field2field4, MAX(field5) AS field5field6field7field8field9 FROM table WHERE field3=3 && field9=class->id GROUP BY field5 UNION SELECT field1field2field2field4field5 AS field5field6field7field8field9 FROM tabe WHERE field3!=5 && field3!=3 && field9=class->id ORDER BY id";
Mar 16 '10 #4
KeredDrahcir
426 256MB
I've found that although the previous solutions works, it doesn't return the first field on the first SELECT statement. I've got three values and it returns the second one I entered. I don't understand why.

Expand|Select|Wrap|Line Numbers
  1. SELECT field1, field2, field3, field4, MAX(field5) AS field5, field6, field7, field8, field9 FROM tables WHERE field3=3 && field9=field9=class->id GROUP BY field5 UNION SELECT field1, field2, field3, field4, field5 AS field5, field6, field7, field8, field9 FROM tables WHERE field3!=5 && field3!=3 && field9=class->id ORDER BY field1;
  2.  
The three rows that have the the 3 of three in field3 have the values of 183, 184 and 223 in field1 which were generated using an autoincrement. When I run the query row 184 comes up and I need to row with the lowest number. Can anyone suggest what to do?
Apr 6 '10 #5
KeredDrahcir
426 256MB
I need to join two statments together but the ORDER BY is different for each statement so I can't use UNION. Is there a way to do it. I'm trying to find something that will do the same as this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table WHERE field3=3 && field9=2 ORDER BY field1 LIMIT 1 UNION SELECT * FROM table WHERE field3!=5 && field3!=3 && field9=2 ORDER BY field1;
  2.  
I know this wont work, but is there a way to get something that would generate the expected result?
Apr 6 '10 #6

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

Similar topics

3
by: Ramses van Pinxteren | last post by:
hello, I have a PHP/Javascript problem. If I want to make a multiple select like this one: $html .= "<td><select name='userid' onChange='changeuserinfo()' multiple size='10'>"; $html .=...
2
by: Wenin | last post by:
I have four total tables. Table One (Documents)- List of Documents. Each record has two fields related to this issue. First field (Document_ID) is the ID of the document, second field is the...
3
by: William Wisnieski | last post by:
Hello Again, I'm really stuck on this one.....so I'm going to try a different approach to this problem. I have a query by form that returns a record set in a datasheet. The user double...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
5
by: Matthew Wells | last post by:
I have a listbox set to simple multi select. For this example, users only select one item at a time. I have command buttons on the form for First, Previous, Next, Last, New (record). The form...
3
newnewbie
by: newnewbie | last post by:
I need help with a query where I am trying to match and display the value (First 18 characters of it) in one table that matches the first 18 characters of the field value in another table. Tables are...
4
by: Chris | last post by:
Hi, I have imported a spreadsheet into an access databsae and have noticed that there are multiple occurances of exactly the same record. I would like to know if there is a way of deleting the...
4
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... ..
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.