Find SQL values

Asked

Viewed 42 times

3

I need help, I have an e-commerce table called product table, I need to list the product name, sales value, products with stock between 20 and 30 and any part of the product name has the letter "A", and I need to sort in this order, starting with the name. As I don’t know SQL made:

SELECT FROM tabelaproduto nomeprod LIKE(%A), valorvenda, WHERE estoqueprod BETWEEN 20 and 30

Is that correct? Because I need to change the name of the product to "promotional" the value of the sale to 10% of the current value, and the stock of the key product "3" to 10 units, when I run the first command, it appears all null.

Thank you =)

1 answer

1


The idea is there, but the syntax is not completely correct. An alternative would be like this:

SELECT nomeprod,
       valorvenda
  FROM tabelaproduto 
 WHERE nomeprod LIKE '%A%'           
   AND estoqueprod BETWEEN 20 and 30
 ORDER BY nomeprod, valorvenda, estoqueprod

The instruction

SELECT nomeprod,
       valorvenda

indicates which information you want to select from the database. If you want, for example, to also list the stock, simply change to:

    SELECT nomeprod,
           valorvenda,
           estoqueprod

The second part indicates which source (table) the information is stored in, and which filters should be applied. When there is no WHERE clause, all records/rows in the table will be listed:

  FROM tabelaproduto      
 WHERE nomeprod LIKE '%A%'           -- produtos cujo nome contém pelo menos um 'A'
   AND estoqueprod BETWEEN 20 and 30 -- e que tenham estoque entre 20 e 30

Finally, the ORDER BY clause specifies the order in which the results should be presented, in which case the results will be sorted by name, sale value and finally by quantity/stock.

ORDER BY nomeprod, valorvenda, estoqueprod

If you want to, for example, sort by name and then by stock available, just change the order of the columns, like this:

ORDER BY nomeprod, estoqueprod
  • 1

    Dude, it really helped me, man, it gave me a North. I was completely lost, I had +- a notion, but his explanation was fantastic. Now for me to change the product, and the price, is that enough? Update tabelaproduto set 
Nomeprod = ‘Promoção’
Valorvenda = valorvenda+(valorvenda*10/100)
Where produtocod = 8;

  • Exactly, that will be enough to upgrade the product.

  • Man, thank you really, really helped me out/

  • You’re welcome, @Geraldosilva

Browser other questions tagged

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