JOIN 查询的 Php pdo 结果

发布于 2024-08-04 10:48:12 字数 549 浏览 2 评论 0原文

我的问题是

我尝试在两个表之间进行简单的 JOIN,这两个表都有 id 字段。我的结果是一个 stdClass 对象,因为我使用 PDO。有谁知道如何区分第一个表的 id 和第二个表的 id?

代码

$sql = "SELECT * FROM products AS p 
        products_categories AS c 
        WHERE c.id = p.category";

$stmt = $connection->prepare($sql);
$stmt->execute();

$products = array();

while($product = $stmt->fetchObject())
    $products[] = $product;

return $products;

如果我尝试使用 $products->id,它将显示 category 表的 id。如果它是一个数组,我可以使用 $products['p.id'] ,我需要一个替代方案。

多谢。

My problem is

I try to do a simple JOIN between two tables, that both have the id field. My result is an stdClass object, as I use PDO. Does anyone know how can I make a difference between the id of the first table and the id of the second table?

Code

$sql = "SELECT * FROM products AS p 
        products_categories AS c 
        WHERE c.id = p.category";

$stmt = $connection->prepare($sql);
$stmt->execute();

$products = array();

while($product = $stmt->fetchObject())
    $products[] = $product;

return $products;

If I try to use $products->id, it will show me the id of the category table. If it was an array, I could use $products['p.id'] , I need an alternative to this.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2024-08-11 10:48:12
$stmt = $db->prepare("SELECT c.car_id, c.car_name, d.dealers_name,m.maker_name
FROM car_details AS c, dealer_details AS d,maker_details AS m
WHERE c.dealer_id=d.dealer_id AND c.maker_id=m.maker_id");

$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo '<pre>';
print_r($rows);
$stmt = $db->prepare("SELECT c.car_id, c.car_name, d.dealers_name,m.maker_name
FROM car_details AS c, dealer_details AS d,maker_details AS m
WHERE c.dealer_id=d.dealer_id AND c.maker_id=m.maker_id");

$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo '<pre>';
print_r($rows);
愿得七秒忆 2024-08-11 10:48:12

您必须为一个表或另一个表中的 id 列指定一个列别名,以使列名不同。

这意味着您不能使用“SELECT *”,您必须拼写出您想要从该表中获取的所有列名称。如果您希望所有这些列都没有别名,那么至少您仍然可以查询category.*

顺便说一句,此类问题是避免在每个表中使用“id”等通用名称作为主键的一个很好的理由。请使用更具描述性的名称,例如 product_idcategory_id。这样就不会出现列名冲突的问题。

You have to give a column alias to the id column from one table or the other to make the column names distinct.

This means you can't use "SELECT *", you have to spell out all the column names you want to fetch from that table. At least you can still query for category.* if you want all those columns without aliasing.

By the way, this sort of problem is a good reason to avoid using a generic name like "id" for your primary key in every table. Instead use a more descriptive names like product_id and category_id. Then you won't have the problem of conflicting column names.

白况 2024-08-11 10:48:12

我使用

select 
    clientes.nombre AS cn, 
    proyectos.nombre AS pn 
FROM 
    proyectos 
        LEFT JOIN clientes 
            ON proyectos.clienteId = clientes.id

This与PDO一起使用 fetch(PDO::FETCH_ASSOC)

I used

select 
    clientes.nombre AS cn, 
    proyectos.nombre AS pn 
FROM 
    proyectos 
        LEFT JOIN clientes 
            ON proyectos.clienteId = clientes.id

This worked with PDO using fetch(PDO::FETCH_ASSOC)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文