How to go through each row of the database compare and update a specific column?

Asked

Viewed 849 times

2

I have a table called PRODUCT and I need to make a comparison between two columns, the column DATA_INICIAL AND DATA_FINAL, for each row travelled, if the product is not in agreement should enter the value 0 in the column PROD_ATIVO.

Rule: If the initial date is greater than the current date or the final date is less than the present date the product should not be displayed.

Product(Exemplification):

Product name: Danoninho
Product display date: 27/03/2017 to 30/03/2017.

3 answers

3

If DATA_FINAL is already smaller than the current date, DATA_INICIAL will never be larger than the current date. So just update the PROD_ATIVO column when DATA_FINAL is less than the current date.

 Update PRODUTO SET PROD_ATIVO='0' where DATA_FINAL < now()

But if there is no certainty that the dates in the bank have been entered correctly you can do so

 Update PRODUTO set PROD_ATIVO='0' where DATA_FINAL < now() or Data_INICIAL > now()
  • There may be registration problems, so the premise is not necessarily valid. Since no restrictions have been reported, they cannot be assumed

  • 1

    I know what it’s like. Employee relapse or bad quality control. But rock.ownar is a smart guy and if you think you should assume you’re right, you’ll add an OR update to start > now(). So it would look like this Update PRODUCT set PROD_ATIVO='0' Where DATA_FINAL < now() or Starting Date > now()

  • Is there a nice way to put constraints to ensure these values between the two columns in Mysql? It’s been a long time since I’ve touched it

  • BETWEEN. About SQL constraints are used to specify the rules for the data in a table. The following restrictions are commonly used in SQL: NOT NULL - UNIQUE - PRIMARY KEY - FOREIGN KEY - CHECK - DEFAULT - INDEX

  • Yeah, I was wondering if you could do something like you do on sql server : http://stackoverflow.com/a/13390565

  • That’s not my area anymore, Sorry!

  • In the final date need to be compared with other fields of another table, example is a table called tb_coupons, how would you do? Update PRODUCT set PROD_ATIVO='0' Where TB_CUPONS.DATA_FINAL < now() or TB_CUPONS.Start_now > now()

  • Here is the problem Leo, the prod_active column is in the products table and comparison should be done in the table tb_coupons column which is the one that has the initial and final dates

  • Yes, misworded question confuses, but I saw in Victor Hartur de Carvalho’s comment that already solved his problem.

Show 4 more comments

2


In the tags says it is mysql so you need to perform an update in the product table, it would be more or less like this:

UPDATE produto
SET PROD_ATIVO = 0
WHERE 
DATA_INCIAL > DATE("now")
OR DATA_FINAL < DATE("now");

Here we say: Update all records and change PROD_ATIVO to 0 where the initial date is greater than the current date (mysql date function) or the final data_is less than the current date;

mysql date functions: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date

EDIT: Assuming that the date fields are like timestamp or date, if they are like string there only converting to dates, the date function does this too...

  • Victor Hartur de Carvalho, here is not working your code with DATE("now") because?

  • Sorry, for mysql the function is NOW(). got confused :)

  • The initial and final date are from the table tb_coupons, but it will be the table products that the column PROD_ATIVO will be updated, then as would the UPDATE?

  • You will have to use alias in tables for example: update produto p set p.prod_ativo = 0 where (tb_cupons.data_inicial > now() or tb_cupons.data_final < now()) and tb_cupons.id_produto = p.id_produto

1

You need to give a UPDATE. In the Tutorial Points has a page on the subject:

Using Mysql:

UPDATE produto
SET PROD_ATIVO =
    CASE
        WHEN DATA_INICIAL <= now() AND DATA_FINAL > now() THEN 1
        ELSE 0
    END

EDIT

As mentioned by @rock.ownar, the problem is update using multiple tables.

You can check the mysql syntax for this; also has the alternative of using the second table only in the clause where, as a subquery.

UPDATE produto, tb_cupons
SET produto.prod_ativo = 0
WHERE
   produto.produto_id = tb_cupons.produto_id AND
   (
       tb_cupons.data_inicial < now() OR
       tb_cupons.data_final > now()
   )
  • @Leocaracciolo, is the message exactly the query I wrote? Or did you hit enter before time? I wrote to Sqlite because for another DBMS the function would be another

  • 1

    yeah, look at the tag there, it’s mysql and sql, similarly "you can’t assume"

  • 1

    @Leocaracciolo that gives not look the tags =D deserved the downvote

  • @Leocaracciolo look, in the original context of the question was only with the sql tag... =\

  • I had seen that I was only sql, kkk

  • The initial and final date are from the table tb_coupons, but it will be the table products that the column PROD_ATIVO will be updated, then as would the UPDATE?

  • How you turn the table TB_CUPONS? She has some id_produto or similar thing?

  • cupon_id and product

Show 3 more comments

Browser other questions tagged

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