Jump to content

Is there any way to force a query to loop unique id's?


Recommended Posts

I have a query like so:

 

SELECT * FROM table WHERE event_id IN (772280,772280)

 

Is there any way to force that query to loop twice and return two results?

 

I'm looping the $result like so and need two results looped:

 

foreach($result as $r){

    // results

}

 

I doubt there's a way to do this, but I figured I'd try throwing it out there and see..

 

Thanks!

Ahh I thought that was gonna work!

 

I guess I left some details out.. There's another issue then.. Not all id's are going to be duplicates..

 

Take this query for example:

 

SELECT * FROM table WHERE event_id IN (772280,772280,772457,772458,772453,772454)

 

I ran your code and it loops all of them 6 times each.. I just need one loop for each id..

 

Thanks a ton for the suggestion requinix!

Hi

 

I can't think of an elegant way of doing it.

 

Best I can come up with is to create a tempory table, insert all the keys within the IN clause and then use an INNER JOIN between the temp table and the actual table.

 

Bit of a faff though.

 

CREATE TEMPORARY TABLE KeyList (Id int(11)  NOT NULL AUTO_INCREMENT, RealField int(11) NOT NULL, PRIMARY KEY (`Id`), KEY `RealField` (`RealField`));

INSERT INTO KeyList (Id, RealField ) VALUES (NULL,772280),(NULL,772280),(NULL,772457),(NULL,772458),(NULL,772453),(NULL,772454);

SELECT * 
FROM table 
INNER JOIN KeyList
ON table.event_id = KeyList.Realfield;

 

All the best

 

Keith

Sorta hard to explain what I'm doin but I'll try..

 

I have a bunch of checkboxes with the value format of id_type.. (i.e. 772280_one)

 

Each id (or group of checkboxes) has 3 different choices for example.. Like so:

 

<input type="checkbox" value="772280_one" />
<input type="checkbox" value="772280_two" />
<input type="checkbox" value="772280_three" />

<input type="checkbox" value="772281_one" />
<input type="checkbox" value="772281_two" />
<input type="checkbox" value="772281_three" />

<input type="checkbox" value="772282_one" />
<input type="checkbox" value="772282_two" />
<input type="checkbox" value="772282_three" />

.....many many more checkboxes with different id's below here as well.....

 

What I was trying to avoid was running a query for each checkbox checked like so:

 

foreach($result as $r){
     // run my query for each choice here that selects the data for that rows id
     // now create a string for the insert here with the new data selected from the rows id
}
// then finally insert the data into the next table

 

I was trying to just run a single query that selects all the info at once.. And then run another single query to insert all the new data at once.. When I create my string of id's and run them trough the loop it only loops duplicates once.. The example I was trying to avoid is working perfectly except it runs a query for each checkbox checked.. I guess it's ok.. I'm rather new to running queries and I was just trying to optimize them as best I can because I'm planning on having thousands of records to query here.. Who knows, maybe it'll be just fine running a query for each id and then doing one query at the end to insert all that data into the new table.. I'd say at max there could be like 25 ids to loop and query at a time..

 

What does anybody think? Good to go? or can it be set up better?

 

Thanks!! I hope that explains a bit more about what I'm trying to accomplish..

 

What does anybody think? Good to go? or can it be set up better?

 

Try using my temp table suggestion.

 

Failing that have a table of options (pretty static) listing the basic value and the full value (ie, 772280_one, 772280_two, 772280_three). So 772280 has 3 (or however many) rows on this table. Have your form return the full valve then JOIN to your main table based on the basic value and check the full value in your WHERE clause.

 

Ie

OptionsTable

FullValue, BasicValue

'772280_one', 772280

'772280_two', 772280

'772280_three', 772280

 

SELECT * 
FROM table 
INNER JOIN OptionsTable
ON table.event_id = OptionsTable.BasicValue
WHERE OptionsTable.FullValue IN ('772280_one','772280_two','772280_three');

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.