Select with 3 conditions and one dependent on a fourth condition

Asked

Viewed 42 times

0

I am populating a chart and for that I need to select the data of each user in the database, table financeiro. Still don’t want to show field records label_chart that are empty, nor fields qdade_stream with a zero value, unless the field label_chart = inicio (in that case the field qdade_stream can have zero value).

SELECT * FROM financeiro WHERE id_usuario = 150 AND label_chart !='' AND (qdade_stream != 0 AND label_chart <>'inicio')
ORDER BY order_chart";

The way I am doing, the query does not return the line whose label_chart=inicio, because its value is zero, the query only obeys the condition of only returning qdade_stream != 0

1 answer

-1

The logic of your query is almost right, only got a little confused in the operators you will use. As it has two conditions that make it possible to display result the ideal will be to use the OR, then I would be:

SELECT * 
FROM financeiro 
WHERE id_usuario = 150 
  AND ((label_chart != '' AND qdade_stream != 0) OR label_chart = 'inicio')
ORDER BY order_chart;

That way he picks up the records with id_registro = 150 and with label_chart and qdade_strem different from void or zero OR just with label_chart equal to the start without taking into account the qdade_stream.

  • I tested this code, but there seems to be some error, because it did not return anything, the graph did not even form.

  • I don’t know what SQL implementation you’re using (Mysql, SQL server, ...) so I don’t know if it might be a problem to use != instead of <>, it might be worth testing it. Otherwise it’s hard to know what might be going on without knowing how your data is.

  • I am using Mysql. I have 4 charts, only this query that can not return what I need, as explained in my statement. Also I can’t know what’s going on. And I don’t understand what you need to know about the database data, because it’s a normal table.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.