1

我将 MySQL 与 Visual Studio 2010 C# 语言一起使用。

我需要查询。

SELECT a field, if this field is null or equals to 0

制作另一个SELECT,如果它<>不同于nullor 0。再做一个SELECT

我也尝试遵循您在上面写标题时出现的建议,但没有。

就像是:

SELECT cart.cod_dependent (if cart.cod_dependent <> 0, Select ass.matricula, ass.nome, ass.situacao_social, ass.categoria, ELSE IF cart.cod_dependent = null Make another select)

每个选择将在不同的表中,但都在同一个数据库中。

------ 更新 10/08/2012 09:59 -------- 现在我正在尝试你所说的Radu Bompa,但它说“在执行命令期间出现致命错误”=\ 。

 MySqlCommand cmd = new MySqlCommand();
  string sql = @"SELECT @cod_dep = cart.cod_dependente FROM carteiras as cart
  IF @cod_dep IS NULL
  BEGIN
  SELECT ass.matricula, ass.nome, ass.situacao_social, ass.categoria FROM associados AS ass INNER JOIN categorias AS cat ON cat.codigo = ass.categoria,
  INNER JOIN situacoes_social AS ss ON ass.situacao_social = ss.codigo
  END
  ELSE
  BEGIN
  SELECT dep.nome, dep.matricula, dep.situacao_social, ss.descricao FROM dependentes INNER JOIN situacao_social as ss dep.situacao_social = ss.codigo
  END
  WHERE cart.numero = @carteira";
  cmd.CommandText = sql;
  cmd.CommandType = CommandType.Text;
  string dependente = "";
  cmd.Parameters.Add(new MySqlParameter("@carteira", MySqlDbType.Int32)).Value = carteira;
  DataSet ds = _dal.Consultar(cmd);
4

1 回答 1

0

也许这样的事情可以工作:

SELECT @SomeValue = cart.cod_dependent FROM cart   
--@SomeValue is a variable, declared for this purpose
IF @SomeValue IS NULL
BEGIN
  SELECT ass.matricula, ass.nome, ass.situacao_social, ass.categoria FROM table2
END
ELSE
BEGIN
  SELECT field FROM table3
END

您也可以尝试使用存储过程

于 2012-10-08T12:46:04.703 回答