axeldrummer
10-15-2005, 10:24 PM
Hello all!
I'm trying to make a program that narrows to a target through mysql selections and subsequent subselections on certain attributes (columns). I hope I can get some help :P
Here is the situation:
I have the attributes/characteristics a = 1, b = 3, c = 4, d = 6 (for example) that are predefined and I'm trying to find the best match out of a database table.
Each row of the table contains these attributes (or columns) a, b, c, d.
First, I randomly select an attribute to match up. Let's say attribute a is selected, then I will search the table where a = 1 as well. This will return a subset matching this requirement. Then, I will randomly choose another attribute (precluding attribute a). I will perform a search on the subset based on the newly chosen attribute. If successful, this returns an even smaller resulting subset.
This process of generating smaller subsets with newly chosen attributes each time continues until nothing is returned (none in subset match). Then, we merely return a random row in the previously generated subset.
$trait_array = array('a', 'b', 'c', 'd');
$x = rand(0, count($trait_array)-1);
$selection = $trait_array[$x]; //randomly chosen column
$query = "SELECT * FROM table WHERE $selection = '$some_value'";
$result = @mysql_query($query);
$row = @mysql_fetch_assoc($result);
if ($row) {
//go further by using another randomly selected attribute ...
} else {
//just randomly select from the subset
}
In order to keep narrowing down subsets, I have to add additional AND clauses to each new SELECT (i.e. a=1 AND d=6 AND ...). How do you do this exactly in code of a mysql query?
Secondly in code, how do I preclude the previous attribute each time that I randomly select the next attribute?
I suspect that this must be a recursive solution, or is there an easier way? Thanks so much for the help!
I'm trying to make a program that narrows to a target through mysql selections and subsequent subselections on certain attributes (columns). I hope I can get some help :P
Here is the situation:
I have the attributes/characteristics a = 1, b = 3, c = 4, d = 6 (for example) that are predefined and I'm trying to find the best match out of a database table.
Each row of the table contains these attributes (or columns) a, b, c, d.
First, I randomly select an attribute to match up. Let's say attribute a is selected, then I will search the table where a = 1 as well. This will return a subset matching this requirement. Then, I will randomly choose another attribute (precluding attribute a). I will perform a search on the subset based on the newly chosen attribute. If successful, this returns an even smaller resulting subset.
This process of generating smaller subsets with newly chosen attributes each time continues until nothing is returned (none in subset match). Then, we merely return a random row in the previously generated subset.
$trait_array = array('a', 'b', 'c', 'd');
$x = rand(0, count($trait_array)-1);
$selection = $trait_array[$x]; //randomly chosen column
$query = "SELECT * FROM table WHERE $selection = '$some_value'";
$result = @mysql_query($query);
$row = @mysql_fetch_assoc($result);
if ($row) {
//go further by using another randomly selected attribute ...
} else {
//just randomly select from the subset
}
In order to keep narrowing down subsets, I have to add additional AND clauses to each new SELECT (i.e. a=1 AND d=6 AND ...). How do you do this exactly in code of a mysql query?
Secondly in code, how do I preclude the previous attribute each time that I randomly select the next attribute?
I suspect that this must be a recursive solution, or is there an easier way? Thanks so much for the help!