R
R
ruRikki2015-08-17 20:42:07
MySQL
ruRikki, 2015-08-17 20:42:07

MySQL SELECT statement - how to set range of values ​​in shorthand format?

View request:

SELECT type_1, type_2, type_3, type_4, type_5, type_6, type_7, type_8, type_9, type_10, type_11, type_12, type_13, type_14, type_15  FROM ".DB_TABLE." WHERE id1='".$id1[$c]."'

How to bring to mind:
SELECT SOME_FUNCTION (type_1,  type_15)  FROM ".DB_TABLE." WHERE id1='".$id1[$c]."'

Which function is suitable? (mysql support) Or any other valid way.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kolegov, 2015-08-17
@ruRikki

This will not work, but you are building a query like that, since at the end there is:
Take advantage of the opportunities provided by the tool with which you build the query. As far as I understand it is PHP.
For example like this:

$select = "";
for($i = 1; $i < 16; $i++){
  $select .= "type_" . $i;
  if($i < 15){
    $select .= ", ";
  }
}

R
ruRikki, 2015-08-18
@ruRikki

Evgeny Kolegov, well, if it's in php, it's better like this)

class RikkiSQL {
  public function RikkiValues($name, $num) {
    $values = "";
    for($i = 1; $i < $num; $i++) {
      $values .= $name.$i.", ";
    }
    $values .= $name.$num;
    return $values;
  }
}

require_once CLASSES."RikkiSQL.class.php";
$sql = new RikkiSQL();
$cost_res2 = db1query("SELECT ".$sql->RikkiValues(building_type_, 15).", ".$sql->RikkiValues(building_cost_, 15).", ".$sql->RikkiValues(building_stotal_, 15).", ".$sql->RikkiValues(building_sliving_, 15).", ".$sql->RikkiValues(building_skit_, 15)." FROM ...

I would like to use sql, I mark it as a solution, thanks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question