如何在MySQL中联接多个表?

编程入门 行业动态 更新时间:2024-10-10 17:29:48
本文介绍了如何在MySQL中联接多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想看看客户从给定制造商那里订购了什么.

I want to see what customers ordered what from a given manufacture.

我有这些表(带有列):

I have theses tables (with columns):

  • 项目(item_num,订单号,库存编号,手册代码,数量等)
  • 库存(stock_num, manu_code ,说明,单价等)
  • 订单( order_num ,order_date, customer_num ,ship_instruct等)
  • 客户( customer_num ,fname,lname,公司,地址1等)
  • items (item_num, order_num, stock_num, manu_code, quantity, etc.)
  • stock (stock_num, manu_code, description, unit_price, etc.)
  • orders (order_num, order_date, customer_num, ship_instruct, etc.)
  • customer (customer_num, fname, lname, company, address1, etc.)

这是我现在的查询,但我相信它会返回某种交叉产品:

This is my query right now, but I believe it is returning a cross product of some sort:

SELECT concat(c.fname," ", c.lname) AS fullname, s.description FROM items i, stock s, customer c JOIN orders o ON o.customer_num=c.customer_num WHERE o.order_num=i.order_num AND i.manu_code = 'ANZ';

哪个返回一个大列表(1000行),其中包含大量重复的整体,

Which returns a big list (1000 lines) with lots of duplicate entires,

Anthony Higgens | baseball gloves Anthony Higgens | baseball gloves . . . . . . Kim Satifer | running shoes

我在做什么错了?

推荐答案

尝试一下:

SELECT DISTINCT concat(c.fname," ", c.lname) AS fullname, s.description FROM customer c INNER JOIN orders o ON c.customer_num = o.customer_num INNER JOIN items i ON o.order_num = i.order_num INNER JOIN stock s on s.stock_num = i.stock_num WHERE i.manu_code = 'ANZ'

更多推荐

如何在MySQL中联接多个表?

本文发布于:2023-10-28 10:50:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何在   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!