L
L
lexstile2019-06-14 00:16:43
PHP
lexstile, 2019-06-14 00:16:43

How to make a nested query on a list id?

There is a table words with fields: id , word

idword
111word 1
222word 2
333word 3
444word 4
555word 5
666word 6

There is a test table with fields: id , word_id_1 , word_id_2 (word_id_* = id from the first table)
idword_id_1word_id_2
one111222
2111333
3111444
4111666

Whether it is possible to make for one request the following:
1. We select from the test table of record where word_id_1 = id; (goal is to get word_id_2 );
If one record were returned here, everything would be simple, from 1 to ~100 records will be returned here.
2. Get records from the words table , where id = word_id_2 (get information on each id from the previous step);
public function getDetailPage($id) {
    $params = [
      'id' => $id, // id из таблицы words
    ];
    return $this->db->row('SELECT * FROM words WHERE id = :id', $params);
  }
  public function row($sql, $params = []) {
    $result = $this->query($sql, $params);
    return $result->fetchAll(PDO::FETCH_ASSOC);
  }

Known: id from the words table (in the example it is equal to 111)
Ultimate goal: to get data on the initially known id from the words table , and also get information on the elements bound to it in the test table.
All data is stored in the first table, in the second - only relationships.
Based on the example, data should come from the words table for all elements except id = 555 .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-06-14
@lexstile

JOIN to help you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question