从会话ID的两个表中获取选择数据

时间:2015-02-01 02:45:05

标签: php mysql pdo

我遇到SELECT查询问题。我基本上试图从两个不同的表中获取信息,我想要客户信息(订单)+客户产品(orders_products),我只想要该特定ID的信息。

两个表都有user_id列。

$conn = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $conn->prepare("SELECT * FROM orders, orders_products WHERE `orders.user_id` = `orders_products.user_id` = :user_id");
$sth->bindValue(':user_id', $_GET['id']);
$sth->execute();
$result = $sth->fetch();

1 个答案:

答案 0 :(得分:0)

SELECT orders.*, orders_products.* FROM orders
JOIN orders_products on orders_products.user_id = orders.user_id
WHERE `orders.user_id` = :user_id;

希望这有帮助:)