如何使用mySQL SELECT IF语句行来联接另一个表? [英] How can I use a mySQL SELECT IF statement row to JOIN another table?

查看:101
本文介绍了如何使用mySQL SELECT IF语句行来联接另一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mySQL SELECT语句中使用IF语句,并且根据IF语句返回的结果,我想加入另一个表.

I am using an IF statement in my mySQL SELECT statement and depending on the returned result from the IF statement I would like to join another table.

例如. 选择名称IF(apple ='brown',color1,color2)作为来自苹果的成熟度JOIN apple_type ON apple_type.color =成熟度

Eg. SELECT name, IF(apple = 'brown', color1, color2) AS ripeness FROM apples JOIN apple_type ON apple_type.color = ripeness

我的问题是我收到错误消息:'on子句'中的未知列'ripeness'.有人知道如何根据IF/ELSE的Select子句中的结果来解决这个问题,以便将另一个表联接起来吗?

My problem is that I am receiving the error msg: Unknown column 'ripeness' in 'on clause'. Does anyone know how I can get around this to join the other table based on the result from the IF/ELSE in the Select clause?

推荐答案

您将不得不在其中重复IF调用.例如:

You will have to duplicate the IF call there. For example:

SELECT
    name,
    IF(apple = 'brown', color1, color2) AS ripeness
FROM
    apples
    JOIN apple_type ON apple_type.color = IF(apple = 'brown', color1, color2)

这篇关于如何使用mySQL SELECT IF语句行来联接另一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆