使用 JOIN 从一个 select 语句中的同一列(可能不同行)中进行选择

发布于 2024-10-25 12:20:19 字数 2579 浏览 2 评论 0原文

首先,我搜索并发现了类似的问题,但没有一个看起来像我需要的。

其次,我做了一个 select 语句,返回我想要的内容,但我没有使用 JOIN,并且如果可能的话,我希望使用 JOIN,使用 JOIN 执行并理解它。

假设我有这些表(经过简化,因为实际表更大):

products: 
fields: idproduct, name, idbaseunit; 
primary key: idproduct
foreign key: idbaseunit references units.idunit
-----------------------------------------------

units: 
fields: idunit, name
primary key: idunit
-----------------------------------------------

prodVSunit:
fields: idprodvsunit, idproduct, idunit, timesbaseunit, cost
primary key: idprodvsunit
foreign key: idproduct references products.idproduct
foreing key: idunit references units.idunit
-----------------------------------------------

这种情况的行为是,我可以为同一产品使用不同的单位,这些单位是基本单位的倍数,但成本并不像倍数那样相对。我的英语不太好,我不知道这是否能很好地解释,所以,这是一个例子:

如果我将大米注册为产品,并将盎司作为其基本单位,我可以将磅(= 16 盎司)作为单元。如果我逐盎司出售,价格可能与磅出售的价格不同,假设每盎司 1.00 美元,每磅 15.45 美元。

在数据库中,它看起来像这样:

insert into units(`idunit`, `name`) values(1,'ounce')
insert into units(`idunit`, `name`) values(2,'pound')

insert into products(`idproduct`, `name`, `idbaseunit`) values(1,'rice',1)
insert into products(`idproduct`, `name`, `idbaseunit`) values(2,'sugar',1)

insert into prodVSunit(`idprodvsunit`, `idproduct`, `idunit`, `timesbaseunit`, `cost`) values(1,1,1,1,1.00)
insert into prodVSunit(`idprodvsunit`, `idproduct`, `idunit`, `timesbaseunit`, `cost`) values(2,1,2,16,15.45)
insert into prodVSunit(`idprodvsunit`, `idproduct`, `idunit`, `timesbaseunit`, `cost`) values(3,2,1,1,0.85)

如果我运行此语句,我会得到我想要的:

select pro.name as product, un.name as unit, pvu.timesbaseunit as 'times base unit', 
aux.name as 'base unit', pvu.cost
from units un, prodVSunit pvu, products pro,
(select prod.idproduct, prod.idbaseunit, unt.name from products prod, units unt
where prod.idbaseunit = unt.idunit) as aux  
where aux.idproduct = pvu.idproduct and pvu.idproduct = pro.idproduct and pvu.idunit = un.idunit

查询返回:

-------------------------------------------------------------------
product______|unit______|times base unit_|base unit_____|cost_
-------------------------------------------------------------------
rice_________|ounce_____|1_______________|ounce_________|1_________
rice_________|pound_____|16______________|ounce_________|15.45_____
sugar________|ounce_____|1_______________|ounce_________|0.85______

请注意(在结果中)“单位”和“基本单位”来自同一表列,但有时来自不同行。

到目前为止一切顺利,但我无法使用 JOIN 来弄清楚,这可能吗???我想要使​​用 JOIN 的主要原因是,使用 2 个嵌套查询很好很简单,但是,如果我有 7 个或 8 个这样的关系,那将会变得一团糟

请原谅我的长篇文章,谢谢进步。

First, I searched and found similar questions but none looks like what I need.

Second, I made a select statement that return what I want, but I'm not using JOINs, and I want, if possible, use JOINs, do it and understanding it with JOINs.

Let's say I have these tables (simplified, because actual tables are bigger):

products: 
fields: idproduct, name, idbaseunit; 
primary key: idproduct
foreign key: idbaseunit references units.idunit
-----------------------------------------------

units: 
fields: idunit, name
primary key: idunit
-----------------------------------------------

prodVSunit:
fields: idprodvsunit, idproduct, idunit, timesbaseunit, cost
primary key: idprodvsunit
foreign key: idproduct references products.idproduct
foreing key: idunit references units.idunit
-----------------------------------------------

The behavior of this is that I can have different units for the same product that are multiples of baseunit but the cost is not relative as the multiples are. My english is not that good, I don't know if this is well explained, so, here is an example:

If I register rice as a product and ounce as baseunit for it, I can have pound (= 16 ounces) as a unit. If I sell it ounce by ounce the price may be different from pounds selling, let's say $1.00 for an ounce, $15.45 for a pound.

In the database it looks like this:

insert into units(`idunit`, `name`) values(1,'ounce')
insert into units(`idunit`, `name`) values(2,'pound')

insert into products(`idproduct`, `name`, `idbaseunit`) values(1,'rice',1)
insert into products(`idproduct`, `name`, `idbaseunit`) values(2,'sugar',1)

insert into prodVSunit(`idprodvsunit`, `idproduct`, `idunit`, `timesbaseunit`, `cost`) values(1,1,1,1,1.00)
insert into prodVSunit(`idprodvsunit`, `idproduct`, `idunit`, `timesbaseunit`, `cost`) values(2,1,2,16,15.45)
insert into prodVSunit(`idprodvsunit`, `idproduct`, `idunit`, `timesbaseunit`, `cost`) values(3,2,1,1,0.85)

If I run this statement I get what I want:

select pro.name as product, un.name as unit, pvu.timesbaseunit as 'times base unit', 
aux.name as 'base unit', pvu.cost
from units un, prodVSunit pvu, products pro,
(select prod.idproduct, prod.idbaseunit, unt.name from products prod, units unt
where prod.idbaseunit = unt.idunit) as aux  
where aux.idproduct = pvu.idproduct and pvu.idproduct = pro.idproduct and pvu.idunit = un.idunit

The query returns:

-------------------------------------------------------------------
product______|unit______|times base unit_|base unit_____|cost_
-------------------------------------------------------------------
rice_________|ounce_____|1_______________|ounce_________|1_________
rice_________|pound_____|16______________|ounce_________|15.45_____
sugar________|ounce_____|1_______________|ounce_________|0.85______

Note that (in the result) 'unit' and 'base unit' are from same table column but sometime from different row.

So far so good, but I cannot figure it out using JOINs, is it possible??? the main reason that I want it with JOINs is that with 2 nested queries is fine and easy, but, if I have 7 or 8 relations like that, it is going to be a mess

Please, forgive me for the long post and Thanks in advance.

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

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

发布评论

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

评论(1

榕城若虚 2024-11-01 12:20:19

您只需加入单位表两次。

我使用 ANSI 连接重写了您的查询,使其更具可读性,然后用第二个单位连接替换了 qub-query:

select pro.name as product, un.name as unit, pvu.timesbaseunit as 'times base unit', 
  baseUnit.name as 'base unit', pvu.cost
from prodVSunit pvu
  inner join units un on pvu.idunit = un.idunit
  inner join products pro on pvu.idproduct = pro.idproduct
  inner join units baseUnit on pro.idbaseunit = baseUnit.idunit

You just need to join to the units table twice.

I rewrote your query using ANSI joins to make it more readable, and then replaced the qub-query with a second join to units:

select pro.name as product, un.name as unit, pvu.timesbaseunit as 'times base unit', 
  baseUnit.name as 'base unit', pvu.cost
from prodVSunit pvu
  inner join units un on pvu.idunit = un.idunit
  inner join products pro on pvu.idproduct = pro.idproduct
  inner join units baseUnit on pro.idbaseunit = baseUnit.idunit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文