There is an array consisting of string data

let's say:

$arr[0]='hello'; $arr[1]='world'; 

It is required to output all records from the table of mysql database, a specific cell of which corresponds to one of the array elements:

 mysql_query("SELECT title FROM $table WHERE value=*Одному из значений*$arr[]"); 

Further more difficult.

For cells in which value is equal to the last element of the array, it is necessary to return not only the title , but also, say, descrition (one or more additional cells). In the example above, for all lines with value value='world' it is required to return not only the title , but also the description .

Further more difficult.

Suppose each entry has its own unique id and parent value (id) to be able to access it. Is it possible to output only those records from the database in which not only the value value corresponds to the value of the array element, but also the value value of the parent corresponds to the value of the previous array element .

Is this even possible in mysql? Thank you in advance.

  • It is required to deduce Further more difficult Further it is more difficult. Is this one request? - BuilderC
  • 1. "... in which not only the value value corresponds to the value of the array element, but also the value value of the parent corresponds to the value of the previous array element." All the same, you should keep the database tables at least in the 3rd NF. 2. I would recommend to thrust the array into a table, and renumber the rows of this table; then it is not necessary to quote VALUE in the request. - BuilderC

3 answers 3

  1. mysql_query("SELECT title FROM $table WHERE value in ('".implode("','", $arr)."')");
  2. mysql_query("SELECT title, description FROM $table WHERE value='".$arr[sizeof($arr)-1]."'");
  3. mysql_query("SELECT title, description FROM $table WHERE value='".$arr[sizeof($arr)-1]."'" and (SELECT id FROM $table where value='".$arr[sizeof($arr)-2]."')); , although here I am not sure.
  • 1st something. As for the second and third, you apparently misunderstood me, but well, in general, I received an answer to my question. Thank. - Getans

Since the question is about MySQL, I answer accordingly for MySQL, respectively, the answer is №1

 SELECT title FROM $table WHERE value in ('hello','world') 

answer number 2

 SELECT title,description FROM $table WHERE value='world' 

answer number 3

Do not quite understand the question, if not difficult, illustrate what you want to get. Keeping trees in MySQL (as well as in other relational databases) is not an easy task. Look at least for a start here: Working with MySQL. Trees

  • No. 1 - an array of course dynamic ... No. 2 - I need to display description ONLY to match the last element of the array. The data in practice will be large and displaying them for each match is inefficient. By number 3 I will not begin to write anything ... - Getans
  • @Getans, for dynamic data, dynamically generate SQL text. (covers №№ 1, 2) - avp

On the 3rd question, something like this:

 $arr = array( 'hello', 'world', 'of', 'mysql' ); $sql = ' CREATE TEMPORARY TABLE `child_value` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`) ); INSERT INTO `child_value` ( `value` ) VALUES ("'.implode('"),("', $arr).'"); CREATE TEMPORARY TABLE `parent_value` SELECT * FROM `child_value`; SELECT `child`.* FROM `table` as `child`, `table` as `parent`, `child_value`, `parent_value` WHERE `child`.`value` = `child_value`.`value` AND `child`.`parent_id` = `parent`.`id` AND `parent`.`value` = `parent_value`.`value` AND `parent_value`.`id` = ( `child_value`.`id` - 1 ) '; echo $sql;