简体   繁体   English

从多个表中选择数据

[英]selecting data from multiple tables

I have this code, and it runs very well. 我有这段代码,它运行得很好。 I have problem on how to select from another table. 我对如何从另一张表中进行select疑问。 This code shows how to select and sum() from 2 different tables. 此代码显示如何从2个不同的表中进行selectsum() What do I do if I am going to select from another table called tblproductlist . 如果要从另一个名为tblproductlist表中select怎么tblproductlist I am selecting 3 tables in one query. 我在一个查询中选择3个表。 I don't know how to do it. 我不知道该怎么做。 I am searching codes, but it's hard for me to apply. 我正在搜索代码,但是我很难申请。

$result = mysql_query("SELECT (
 SELECT SUM(s.total)-SUM(r.total) FROM rsales AS s WHERE r.pcode=s.pcode
 ) as total, r.pcode 
 FROM rreturn AS r 
 GROUP BY r.pcode;");

I tried this code but it returns an error. 我尝试了此代码,但返回了错误。

$result = mysql_query("SELECT productlist.*,
(SELECT SUM(s.total)-SUM(r.total) 
  FROM rsales AS s 
  WHERE r.pcode=s.pcode) as total, 
r.pcode FROM rreturn AS r 
GROUP BY r.pcode;");

for example I have this ff value. 例如我有这个ff值。

tblproductlist tbl产品清单

pcode | pname |
111   | wire  |

tablersales 桌布

|  total    | pcode |
|  200      |  111  |
|  200      |  111  |

tablerreturn 表返回

|  total    | pcode |
|  200      |  111  |

so the output after subtracting its total must be something like this. 因此减去总和后的输出必须是这样的。

pcode | pname | total
111   | wire  | 200   |

Very loosely speaking, the results of a query need to (a) have the same straucture, (b) be somehow related. 粗略地说,查询的结果需要(a)具有相同的结构,(b)以某种方式相关。 The resultset you appear to be trying to build has neither property (at the very least, all pieces of your querys should return the same number of rows)It appears that you need to gain better understanding of sql, besides explaining a little better what you need to do downstream. 您似乎要构建的结果集都不具有属性(至少,所有查询段应返回相同的行数)似乎除了需要更好地解释自己的内容外,还需要更好地了解sql需要做下游。

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

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