Jump to content

PHP And MySQL Queries


berridgeab

Recommended Posts

Hello

 

I have a design issue that I have never really known how to approach solving. Its a generic question not related to any specific scenairo. When I need to query a database table i.e. say a generic product table, sometimes I need to restrict what rows I return. I.e. I may only want products that have not been sold.

 

So my SQL string would look somthing like

$query = "SELECT * FROM products WHERE status != 'Sold';

Sometimes I perform more complicated queries on other tables. Say for instance I may need to join my product table to a theoretical orders table. This would result in a SQL query like

$query = "SELECT * FROM orders LEFT JOIN products ON products_id = orders_product_id WHERE order_id = 1 AND status != 'Sold';

My problem is that I now have 2 different pieces of SQL code in 2 different places. Both snippets of code have something in common

status != 'Sold' /*This is the only bit im really interested in*/

What is the best way to maintain bits of SQL code that needs to be the same across the entire site?

Link to comment
Share on other sites

What is the best way to maintain bits of SQL code that needs to be the same across the entire site?

 

you could create a 'view' in your database for the products table that only 'matches' status != 'Sold' and use the view name in the queries instead of the products table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.