Ben iki tablodan veri almak istiyorsanız en iyi yöntem nedir?

0 Cevap php

İki tablo var,

agent(id, name, password, ..., shop_id);
shop(id, name, address, ...);

Now I want to fetch the shop name for an agent, what should I do? Should I use two sequence sql queries,

<?php
$qstr = <<<SQL
SELECT * FROM agent WHERE id=$id;
SQL;
$result = $conn->query($qstr);
$row = $result->fetch_assoc();
foreach ($row as $key => $value)
    $$key = $value;

$qstr = <<<SQL
SELECT name FROM shop WHERE id=$shop_id;
SQL;
$result = $conn->query($qstr);
$row = $result->fetch_assoc();
$shop_name = $row['name'];
?>

Yoksa, bu seçkin bir görünüm oluşturmanız gerekir

CREATE VIEW f_agent AS SELECT agent.id AS id ,agent.name AS name, shop.id AS shop_id, shop.name AS shop_name FROM agent, shop WHERE agent.shop_id=shop.id;

Veya sadece sql kullanmanız gerekir,

SELECT agent.id AS id ,agent.name AS name, shop.id AS shop_id, shop.name AS shop_name FROM agent, shop WHERE agent.shop_id=shop.id and agent.id=$id;

Bunu yapmanın en iyi yolu nedir?

Teşekkürler.

0 Cevap