如何从两个表中检索数据?[英] How to retrieve data from 2 tables?

本文是小编为大家收集整理的关于如何从两个表中检索数据?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这里有人知道如何加入2个表?我一直在语法方面遇到错误,我不知道错误和在哪里.我想要的就是从不同表中获取这些特定数据.请帮助!

<?php
echo"<ul>";
$sql="select product_title.products, qty.cart from  products Inner Join cart on product_id.procucts=p_id.cart ";

$query = mysql_query($sql);

while($row = mysql_fetch_array($query))
    echo"
    <center>Summary of orders <br><br>
        <li>Your Order: $row[0]</li>
        <li>Quantity: $row[1]</li>
    </center><br/>";
echo"</ul>";
?>

推荐答案

表前缀应首先.因此您的查询希望如下:

$sql="select products.product_title, cart.qty from products Inner Join cart on procucts.product_id=cart.p_id ";

其他推荐答案

这是对第一个答案的解释(kinjamistry).

您必须永远是代码语法!您必须始终在大写中写入保留的SQL单词.您必须始终格式化代码.这避免了将来的此类问题和错误.

SELECT
        cart.qty,
        products.product_title
    FROM cart /* select first from cart beacause here cart is "parent" data */
    INNER JOIN products /* join "dependency" data */
        ON products.product_id = cart.p_id /* compare as dependency<->parent (like native: you<-above->friend) */

其他推荐答案

当您select&join clause table列名称时,您会误解.它应该首先出现table name,而不是这样的列名.

$sql = "SELECT products.product_title, cart.qty FROM products INNER JOIN cart ON products.product_id = cart.p_id";

更多关于join: mysql join

本文地址:https://www.itbaoku.cn/post/597365.html

问题描述

Does someone here know how to join 2 tables? I keep having an error in terms of the syntax I don't know what and where is the error. All I want is to get this specific data from different tables. Please Help!

<?php
echo"<ul>";
$sql="select product_title.products, qty.cart from  products Inner Join cart on product_id.procucts=p_id.cart ";

$query = mysql_query($sql);

while($row = mysql_fetch_array($query))
    echo"
    <center>Summary of orders <br><br>
        <li>Your Order: $row[0]</li>
        <li>Quantity: $row[1]</li>
    </center><br/>";
echo"</ul>";
?>

推荐答案

Table Prefix should be first. So that your query would like as follow:

$sql="select products.product_title, cart.qty from products Inner Join cart on procucts.product_id=cart.p_id ";

其他推荐答案

This is explain for first answer (by KinjaMistry).

You must be code grammar always! You must write reserved SQL words in uppercase always. You must format your code always. This avoid such problems and errors in future.

SELECT
        cart.qty,
        products.product_title
    FROM cart /* select first from cart beacause here cart is "parent" data */
    INNER JOIN products /* join "dependency" data */
        ON products.product_id = cart.p_id /* compare as dependency<->parent (like native: you<-above->friend) */

其他推荐答案

You mistake when you select & join clause the table column name. It should be appears first table name than column name like this.

$sql = "SELECT products.product_title, cart.qty FROM products INNER JOIN cart ON products.product_id = cart.p_id";

More about join: MySQL join