1

I got mysql table like this

id | type | number
1  |  c   |  2
2  |  c   |  10
3  |  c   |  20

Also i got PHP array with values:

$array[c] = 5;
$array[d] = 10;

I wanna do something like this

( SELECT * FROM table WHERE number >= $array[ type ] ) 

so that type could be taken from mysql column somehow and used for finding correct value from array.

Thats kinda tricky, but I'm not sure how better I could ask.

5
  • Since $array[c] is an integer, what exactly prevents you from using it as parameter? Commented May 29, 2013 at 16:18
  • 2
    where type = c and number >= 5 for the $array[c] = 5; you mean? Commented May 29, 2013 at 16:19
  • @YourCommonSense that's all I can make of it as well. Commented May 29, 2013 at 16:20
  • 1
    I think he means, for all integers in his array, get the corresponding value based on the key, which is the value in the type field in the database... So if type = c, number > 5, if type was d, number > 10, but he'd need it all in a single statement. The only way I can see to do it is with PHP generating a massive WHERE clause with lots of ORs. It's a bit weird to wrap your head around: WHERE (type = c AND number > 5) OR (type = d AND number > 10) Commented May 29, 2013 at 16:20
  • Mathew is right, I cannot use where type = c and number >= 5, because i have like 6 types and numbers from 1 to 50 and i wanna take less mysql rows from my database. Commented May 29, 2013 at 16:25

4 Answers 4

1

This isn't the most elegant way but something like this?

$where = "WHERE ";

foreach($array as $key => $value)
{
    $where .=  "(type = $key AND number >= $value) OR";
}

$where = substr($where, 0, strlen($where)-2);

You'd have to attach that to your select statement and then run the query obviously.

Hopefully that allows someone else to catch on and provide a more elegant solution.

Sign up to request clarification or add additional context in comments.

3 Comments

I tested this one and it works. Also its quite simply. So this answer best answers my question, i guess ;] though, I didnt test kickstart's answer, because its longer.
Both would do the job. This solution would likely give SQL that is easier to read at a glance. My solution avoids repeated ORs, and if the number of checks gets too large can easily be changed to use a temp table rather than selecting values to do the join. If I were using this solution I would be tempted to put each check into an array and then implode the array to create the WHERE (as I did in my solution), but mainly as I have a bit of an aversion to building up a string and then chopping off the extra OR. I substr is used then no need to use strlen, substr accepts a negative 3rd parameter
I agree with @Kickstart, my method isn't a very practical one as it will create a massive string of SQL OR statements stuck together. It's something I would normally avoid but typed out like this to help others understand the problem.
0

Try this:

$type = 'c';
$query = "SELECT * FROM table WHERE number >= " . intval( $array[ $type ] ) . " AND type = '" . $type . "'";

1 Comment

what should I do if i'm taking more than one type? acctually i'm taking 6 types from mysql.
0

try to do it in two steps: before use an sql query to put in an array the values of type

then in a while

  ( SELECT * FROM table WHERE number >= $array[$i] )

where $i is the index of the while loop

Comments

0

Possibly a bit better than using lots of WHERE clauses would be something like this:-

SELECT *
FROM table a
INNER JOIN
    (SELECT 'c' AS RowType, 5 AS RowVal
    UNION
    SELECT 'd', 10) Sub1
ON a.type = Sub1.type AND a.number >= Sub1.RowVal

Set up a subselect which is just getting the constants, and then do a join between that subselect and your existing table.

In php done something like this:-

<?php
$SubQuery = array();
foreach ($array AS $key=>$value)
{
    $SubQuery[] = "SELECT '$key' AS RowType, $value AS RowVal";
}

$sql = "SELECT *
FROM table a
INNER JOIN (".implode(" UNION ", $SubQuery).") Sub1
ON a.type = Sub1.type AND a.number >= Sub1.RowVal";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.