简体   繁体   English

MYSQL IF语句到不同的WHERE子句

[英]MYSQL IF statement to different WHERE clause

I'm trying to solve a question on my app, i have this query to try to make it work. 我正在尝试解决我的应用程序上的一个问题,我有这个查询来尝试使其工作。

SELECT * FROM articles
(IF articles.type = 1, INNER JOIN members ON members.id_member = articles.author_id,
INNER JOIN company ON company.id_company = articles.author_id) AS user
WHERE articles.tipe = '1' AND  user = '1';

I have a systems that post articles, but the same user can post on his name or in the name of a company that he has created, so, What i'm trying to do is, if the articles.tipe is type 1, then the article was published by his company, and if otherwise, it's type 0, then it's posted by him on his own name, if i only use 1 of them, they will get mixed, i've tryed different approches but none of them seem to work for me, can anybody help please? 我有一个发布文章的系统,但是同一用户可以以他的名字或他创建的公司的名义发布文章,因此,我要执行的操作是,如果articles.tipe为类型1,则该文章由他的公司发布,如果不是,则为0,然后由他以他自己的名字发布。如果我只使用其中的1个,它们将会混合在一起,我尝试了不同的方法,但似乎都没有为我工作,有人可以帮忙吗?

You can left join both tables. 您可以保留两个表的联接。 For each articles record, one of the joined records will have values, the other will have all nulls. 对于每个商品记录,一个合并的记录将具有值,另一个将具有所有空值。 You can use coalesce on the columns or a case expression. 您可以在列或案例表达式上使用合并。

SELECT a.*, coalesce(m.name,c.name) as username
FROM articles a
LEFT JOIN members m ON m.id_member = a.author_id and a.type = '0'
LEFT JOIN company c ON c.id_company = a.author_id and a.type = '1'
WHERE a.user = '1';

you should use inner join to fetch only articles which have member id and company id for article type=1. 您应该使用内部联接来仅获取具有文章类型= 1的成员ID和公司ID的文章。 so article_type condition will come in where condition and we will use inner join with member and company table. 因此article_type条件将出现在条件所在的位置,并且我们将内部联接与member和company表一起使用。

Following query will give all the articles which have article type 以下查询将提供所有具有文章类型的文章

select * from articles INNER JOIN members ON members.id_member = articles.author_id INNER JOIN company ON company.id_company = articles.author_id  where articles.type=1 and user=1

please add some more clarification if you think i am not understanding your question properly. 如果您认为我无法正确理解您的问题,请添加更多说明。 Hope this helps you. 希望这对您有所帮助。

To get records for both article_type=0 and article_type=1 with conditional joins based on article type, here is the query: 要获取基于文章类型的条件连接的article_type = 0和article_type = 1的记录,请执行以下查询:

select * from (

  select article.type,article.name,member.name as user from articles INNER JOIN members                         ON members.id_member = articles.author_id where article_type=0

 UNION ALL

 select article.type,article.name,company.name as user from articles INNER JOIN members       ON members.id_member = articles.author_id INNER JOIN company ON company.id_company = articles.author_id  where article_type=1

) as tmptbl; )作为tmptbl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM