【问题标题】:mysql joing tables [closed]mysql加入表[关闭]
【发布时间】:2012-12-17 05:00:34
【问题描述】:

我想从这三个表中提取日期。

users有三列

uid, username , data(text) 

users_order有三列

uid, orders_id , users_email

order_products有三列

orders_id, product_id, product_name

我想使用product_id 作为参考/搜索从这三个表中提取用户信息。

如果 product_id = 5

查询将显示

uid; username; users_email; orders_id; product_name; data (text)

对于这种情况,什么是正确的 sql 查询。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:
    select a.uid,a.username,b.user_email, b.orders_is, c. product_name
    from users a, users_orders b, order_products c
    where a.uid = b.uid and b.order_id = c.order_id and product_id = 5
    

    这应该可以满足您的需求

    【讨论】:

      【解决方案2】:

      您必须在最后使用两个 JOINS 和一个 where 条件。

      • 首先在 users 和 users_order 表之间加入 uid

      • users_order 和 product_orders 之间第二次加入 orders_id

      • 以及查询结束时的 where 条件 WHERE product_id=5

      为表格使用别名。

      【讨论】:

        猜你喜欢
        • 2016-09-25
        • 2020-12-10
        • 2012-07-21
        • 1970-01-01
        • 2013-01-24
        • 1970-01-01
        • 2014-10-14
        • 2010-09-09
        • 1970-01-01
        相关资源
        最近更新 更多