473,406 Members | 2,867 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,406 software developers and data experts.

Passing multiple IDs to the WHERE clause of an SQL query

25
i wrote code like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. $Query1="select CarID from t_car_booking_info where PickupDate in('".$_POST['pickupdate']."','".$_POST['dropoffdate']."') and DropoffDate in('".$_POST['dropoffdate']."','".$_POST['pickupdate']."')";
  3. $result1=mysql_query($Query1);
  4. $nrows = mysql_num_rows($result1);
  5.  
  6. while($row=mysql_fetch_array($result1))
  7.      { 
  8.        for($i=0;$i<mysql_num_rows($result1);$i++) 
  9.           {  
  10.             $CarID=mysql_result($result1,$i,"CarID");  
  11.           }
  12.      $Query2="select * from t_car_info where CarID!='".$CarID."'";
  13.     }
  14.  
  15.  
  16.  

in that if query1 returns more than one carid how can i pass all carids at a time in the query2
Sep 18 '07 #1
1 2169
pbmods
5,821 Expert 4TB
Heya, MMR.

Changed thread title to better describe the problem.

First off, instead of doing this:
Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_array($result1))
  2.      {
  3.        for($i=0;$i<mysql_num_rows($result1);$i++)
  4.           { 
  5.             $CarID=mysql_result($result1,$i,"CarID"); 
  6.           }
  7.      $Query2="select * from t_car_info where CarID!='".$CarID."'";
  8.     }
  9.  
Get in the habit of doing this:
Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_assoc($result1))
  2.     {
  3.      $CarID = $row['CarID'];
  4.      $Query2="select * from t_car_info where CarID!='".$CarID."'";
  5.     }
  6.  
Try using MySQL's NOT IN subclause, combined with PHP's implode() builtin:

Expand|Select|Wrap|Line Numbers
  1. $_values = array();
  2. while( $_row = mysql_fetch_assoc($result1) )
  3. {
  4.     $_values[] = $_row['CarID'];
  5. }
  6. mysql_free_result($result1);
  7.  
  8. $_sql = "
  9. SELECT
  10.         *
  11.     FROM
  12.         `t_car_info`
  13.     WHERE
  14.         `CarID`
  15.             NOT
  16.             IN
  17.             (
  18.                 '" . implode("',\n\t\t\t\t'", $_values) . "'
  19.             )";
  20. $_res = mysql_query($_sql);
  21.  
Sep 18 '07 #2

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

Similar topics

6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
1
by: droope | last post by:
I am trying to create a proc with a parm @whereclause that will have a value passed in of one to many policy ids. The parm coming into the proc looks like this, ('000000000108', '000000000106',...
9
by: Ed_No_Spam_Please_Weber | last post by:
Hello All & Thanks in advance for your help! Background: 1) tblT_Documents is the primary parent transaction table that has 10 fields and about 250,000 rows 2) There are 9 child tables with...
2
by: Matthew Clement | last post by:
I'm currently building a form (called frmReports) to set the criteria for a query, but I'm having some trouble with syntax and hope that one of the guru's here can help me achieve what I'm do. ...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
1
by: isaphrael | last post by:
I have a select query that has a WHERE clause that changes depending on the button that a user presses to search. this is the code that executes everytime one of 3 different search buttons are...
4
by: carl.barrett | last post by:
Hi, I have a list box that displays 2 columns. Behind it sits a query with five columns. These are Column1 (DOB), column2 (a concatenated string of Surname Forname, Title), Column3 (Surname),...
11
by: Randy Harris | last post by:
I have been using "IN" to query tables in Oracle from MS Access very successfully. Select Field FROM MyTable IN [ODBC...etc Works great if there is only one table involved. Anyone know how I...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
3
by: manjeetgrang | last post by:
to pass multiple parametes in VB Data Report -------------------------------------------------------------------------------- to pass multiple parametes in VB Data Report query(specially date...
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.