Mysql select where t4.name=x 并且 t2 或 t3 有一个 t1.user !? :)

发布于 2024-10-12 15:08:43 字数 680 浏览 1 评论 0原文

为了解释我的问题,让我们想象 4 个表。

users - (userid, user.data)

buy - (buyid, productid, userid, buy.conditions)

sell - (sellid, productid, userid, sell.conditions)

产品 - (productid, Product.data)

我想做一个选择,当有用户购买或出售名称上带有 X 的产品时,返回所有用户和产品数据。

SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       buy, 
       sell, 
       products 
WHERE  ( buy.userid = users.userid 
          OR sell.userid = users.userid ) 
       AND ( buy.productid = product.productid 
              OR buy.productid = product.productid ) 
       AND product.DATA LIKE '%x%'  

这行不通,但我相信你明白我想要做什么。

To explain my problem lets imagine 4 tables.

users - (userid, user.data)

buy - (buyid, productid, userid, buy.conditions)

sell - (sellid, productid, userid, sell.conditions)

product - (productid, product.data)

I want to do a select that returns all the users and product data when there is a user that have products with X on the name to buy or sell.

SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       buy, 
       sell, 
       products 
WHERE  ( buy.userid = users.userid 
          OR sell.userid = users.userid ) 
       AND ( buy.productid = product.productid 
              OR buy.productid = product.productid ) 
       AND product.DATA LIKE '%x%'  

this doesnt work but I belive you get the idea of what I am tryng to do.

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

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

发布评论

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

评论(3

忆依然 2024-10-19 15:08:43

尝试像下面这样的联合:

SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       buy, 
       products 
WHERE  (buy.userid = users.userid) 
  AND  (buy.productid = product.productid)
  AND  product.DATA LIKE '%x%'  
UNION 
SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       sell, 
       products 
WHERE  (sell.userid = users.userid ) 
  AND  (sell.productid = product.productid ) 
  AND  product.DATA LIKE '%x%'  

Try a union like below:

SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       buy, 
       products 
WHERE  (buy.userid = users.userid) 
  AND  (buy.productid = product.productid)
  AND  product.DATA LIKE '%x%'  
UNION 
SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       sell, 
       products 
WHERE  (sell.userid = users.userid ) 
  AND  (sell.productid = product.productid ) 
  AND  product.DATA LIKE '%x%'  
記柔刀 2024-10-19 15:08:43

非常感谢大家!

在朋友的帮助下最终做了这样的事情:


SELECT USER.DATA, 
      product.DATA, origem
From
(
SELECT 'b' Origem,
   productid, 
   userid 
FROM buy
union all
SELECT 's' Origem,
   productid, 
   userid 
FROM sell
) tabelas
inner join users on users.userid=tabelas.userid
inner join products on products.productid=tabelas.productid
Where product.DATA LIKE '%x%'

Thanks a lot guys!

With the help from a friend end up making something like this:


SELECT USER.DATA, 
      product.DATA, origem
From
(
SELECT 'b' Origem,
   productid, 
   userid 
FROM buy
union all
SELECT 's' Origem,
   productid, 
   userid 
FROM sell
) tabelas
inner join users on users.userid=tabelas.userid
inner join products on products.productid=tabelas.productid
Where product.DATA LIKE '%x%'
林空鹿饮溪 2024-10-19 15:08:43

您可以使用 EXISTS 子句:

SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       products 
WHERE  ( EXISTS (SELECT * 
             FROM   buy 
                    INNER JOIN product 
                      ON ( buy.productid = product.productid ) 
             WHERE  buy.userid = users.userid 
                    AND product.DATA LIKE '%x%') ) 
        OR ( EXISTS (SELECT * 
                 FROM   sell 
                        INNER JOIN product 
                          ON ( sell.productid = product.productid ) 
                 WHERE  sell.userid = users.userid 
                        AND product.DATA LIKE '%x%') 

        AND products.userid = users.userid)

You could use the EXISTS clause:

SELECT userid, 
       USER.DATA, 
       productid, 
       product.DATA 
FROM   users, 
       products 
WHERE  ( EXISTS (SELECT * 
             FROM   buy 
                    INNER JOIN product 
                      ON ( buy.productid = product.productid ) 
             WHERE  buy.userid = users.userid 
                    AND product.DATA LIKE '%x%') ) 
        OR ( EXISTS (SELECT * 
                 FROM   sell 
                        INNER JOIN product 
                          ON ( sell.productid = product.productid ) 
                 WHERE  sell.userid = users.userid 
                        AND product.DATA LIKE '%x%') 

        AND products.userid = users.userid)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文