简体   繁体   English

MQL,PHP是否从两个表中全部选择?

[英]MQL, PHP select all from two tables?

So i have two tables: 所以我有两个表:

order_product

--------------------------------------------------
| ProductID     | Quantity         
--------------------------------------------------

products

-------------------------------------------------------------
| ProductID     | productname | Desc | Price | Stock | Image     
------------------------------------------------------------

and i need to get all of the same product ID, show their quantity then times that by their price and show the grand total of all. 我需要获取所有相同的产品ID,显示其数量,然后乘以其价格乘以,并显示所有的总计。

My problem is i'm trying to show a checkout page which shows everything in a list, but how do i combine the two tables? 我的问题是我要显示一个结帐页面,该页面显示列表中的所有内容,但是如何合并两个表? Also, there are no foreign keys for the first table. 另外,第一个表没有外键。

I need this in an sql statement as well preferably, like: 我最好在sql语句中也需要它,例如:

$sql = 'SELECT...'

Would this work? 这行得通吗?

$sql = "SELECT * FROM order_products
UNION
SELECT * FROM products"

If so, how do i know which row is which? 如果是这样,我怎么知道哪一行?

My desired output is all entries, now looking like this: 我想要的输出是所有条目,现在看起来像这样:

ProductID     | Quantity   | Productname | Desc | Price | Stock | Image

You need a classical JOIN clause: 您需要一个经典的JOIN子句:

   SELECT * 
     FROM products
LEFT JOIN order_products on products.ProductId = order_products.ProductId
select table1.ProductID
     , table1.Quantity
     , table2.Productname
     , table2.Desc
     , table2.Price
     , table2.Stock
     , table2.Image 
FROM table1 
JOIN table2 ON table1.productid=table2.productid

My problem is i'm trying to show a checkout page which shows everything in a list, but how do i combine the two tables? 我的问题是我要显示一个结帐页面,该页面显示列表中的所有内容,但是如何合并两个表?

You need to simply use mysql JOIN to show cart items. 您只需要简单地使用mysql JOIN来显示cart项目。

Your table data should be like as i added in demo. 您的表格数据应该像我在演示中添加的一样。

See SQL Fiddle Demo 请参见SQL Fiddle演示

SELECT 
  o.id 'orderId',
  o.ProductID 'pid',
  SUM(o.Quantity) 'qty',
  p.productname  'product',
  p.`Price` 'price' 
FROM
  order_product o 
  INNER JOIN products p 
    ON p.`ProductID` = o.`ProductID` 

Edit Required output 编辑所需的输出

My desired output is all entries, now looking like this: 我想要的输出是所有条目,现在看起来像这样:
ProductID | 产品编号| Quantity | 数量| Productname | 产品名称 Desc | Desc | Price | 价格 Stock | 库存 Image 图片

Modify above query's SELECT part SELECT tablename.your_column ..... 修改以上查询的SELECT部分SELECT tablename.your_column .....

Not sure exactly what's the output you wish to get is but you should use something like this 不确定您希望获得的输出是什么,但是您应该使用类似这样的东西

SELECT o.ProductID, o.Quantity, p.Price, o.Quantity * p.Price
FROM order_product o
LEFT JOIN products p ON o.ProductID = p.ProductID

Use inner join. 使用内部联接。

Select * from order_products op INNER JOIN products p ON p.ProductID = op.ProductID;

wrt your need, the above query needs to be modified to:- 根据您的需要,上述查询需要修改为:-

 "SELECT *,
         SUM(op.quantity * p.price) AS grandTotal
    FROM order_products op
    INNER JOIN PRODUCT p ON p.ProductID = op.ProductID WHERE p.ProductID =".$prodId(your php variable of product id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM