简体   繁体   English

用if语句连接另一个表

[英]join another table with if statement

I want to join another table if a field of current table equals to some specific value. 如果当前表的field等于某个特定值,我想加入另一个表。
For example : ( this is not mysql syntax ) 例如:(这不是mysql语法)

SELECT * FROM `products` ( IF products.is_package IS NOT NULL THEN INNER JOIN `packages` )  

Table products: 餐桌产品:

 +----+-----------+
 | id | is_package|
 +----+-----------+
 | 1  |   1       | // which joins package with id 1
 +----+-----------+
 | 2  | NULL      |
 +----+-----------+
 | 3  |   2       | // which joins package with id 2
 +----+-----------+

Is it possible to do this in mysql? 是否可以在mysql中执行此操作?

 SELECT * FROM `products` LEFT JOIN `packages` 
  ON products.is_package = packages.id

Your if condition you can just put it in WHERE clause .try this 您的if条件可以将其放在WHERE子句中。

  SELECT * FROM `products` LEFT JOIN `packages` 
  ON your_clause_here

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

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