D
D
darknet372017-03-06 08:57:48
Yii
darknet37, 2017-03-06 08:57:48

How to make a double query to the database?

I have two tables: in one category and in another products.
How can I display products belonging to the desired category?
fe385146e0d440f287fc9b49d10e2e68.png
fb5414a0bcce461186a563835ed2fa45.png
If the links were of the form: site.ru/category/id/3 - then everything is clear with this, I would receive a GET parameter, because it is in the product table.
But what about the cases with: site.ru/postelnoe-bele-gost ?
I'm confused, please help. Thank you in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan, 2017-03-06
@darknet37

$sql = "SELECT * FROM `product_table` WHERE `category_id` IN (SELECT `id` FROM `category_table` WHERE `alias`={$alias})";

M
Maxim Fedorov, 2017-03-06
@qonand

use the standard relation engine

D
Denis Skripchenko, 2017-03-07
@dskripchenko

$sql = <<<SQLSTR
SELECT `pt`.* FROM `product_table` AS `pt` 
INNER JOIN `category_table` AS `ct` ON `ct`.`category_id` = `pt`.`category_id`
WHERE `ct`.`alias` = :alias
SQLSTR;
$resultArray = Yii::$app->db->createCommand($sql)->bindValues([':alias'=>$alias])->queryAll();
$resultProductList = Product::findBySql($sql,[':alias' => $alias])->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question