0

I have a shop set up with lots of tables that are joined together in various ways, as per usual.

In my products table, I have a field called 'status'. If the status = 4, then the product is archived.

I want to ensure that no queries ever return anything with a status of 4. Right now I'm about to add a AND status <> 4 to every SQL query I can find.

Is there a better way to do this, or is that the only way?

3
  • 2
    That is pretty much the only way, yes.. Except if you make a table that holds the archived products. Then you just move them from the main table to the archive table. Dec 17, 2012 at 15:18
  • How many products do you have? Luc has the correct answer. But if you have lots and lots of products, you might want to move them to a different table for performance reasons. Dec 17, 2012 at 15:22
  • Luc has indeed a great point. I totally forgot about views.. That might be a good option here. Dec 17, 2012 at 15:29

2 Answers 2

6

You can create a view that doesn't show status = 4 and use that view in your query's instead.

0

Presumably changing a product's status between archived and unarchived is a pretty rare operation, while selecting from your product table is extremely common. Therefore you should make a table archived and move all archived products there.

This would be the best-performing solution.

If you also occasionally wanted to view all products, whether archived or not, then you could also make a view that combines the archived and products tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.