Select statement

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
faris10
Forum Newbie
Posts: 9
Joined: Sun Dec 11, 2005 6:25 pm

Select statement

Post by faris10 »

Hi All,
I have 2 tables producst and products_description
I want to use select statement to select products.Productname,products.URL,products_description.description where products.products_id=products_description.products_id
There is something wrong with my statemnet in PHP , can anyone reply with correct syntax. I'm using PHP 5 and mysql 5
Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$sql = "SELECT products.Productname, products.URL, products_description.description 
        FROM products, products_description 
        WHERE products.products_id = products_description.products_id";
?>
faris10
Forum Newbie
Posts: 9
Joined: Sun Dec 11, 2005 6:25 pm

Post by faris10 »

Everah wrote:

Code: Select all

<?php
$sql = "SELECT products.Productname, products.URL, products_description.description 
        FROM products, products_description 
        WHERE products.products_id = products_description.products_id";
?>
Thanks I see where my mistake was
faris10
Forum Newbie
Posts: 9
Joined: Sun Dec 11, 2005 6:25 pm

Post by faris10 »

Everah wrote:

Code: Select all

<?php
$sql = "SELECT products.Productname, products.URL, products_description.description 
        FROM products, products_description 
        WHERE products.products_id = products_description.products_id";
?>
What if I do not want to include all the products where product name is empty ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

faris10 wrote:What if I do not want to include all the products where product name is empty ?
Lets use some logic...

Code: Select all

<?php
// Get all products
$sql = "SELECT products.Productname, products.URL, products_description.description 
        FROM products, products_description 
        WHERE products.products_id = products_description.products_id";
?>
Now lets say we want only products whose description has something in it

Code: Select all

<?php
$sql = "SELECT products.Productname, products.URL, products_description.description 
        FROM products, products_description 
        WHERE products.products_id = products_description.products_id
        AND products_description.description <> ''
        # or you can use...
        #AND products_description.description IS NOT NULL";
?>
Can you apply this logic to other criteria?
faris10
Forum Newbie
Posts: 9
Joined: Sun Dec 11, 2005 6:25 pm

Post by faris10 »

Now lets say we want only products whose description has something in it

Code: Select all

<?php
$sql = "SELECT products.Productname, products.URL, products_description.description 
        FROM products, products_description 
        WHERE products.products_id = products_description.products_id
        AND products_description.description <> ''
        # or you can use...
        #AND products_description.description IS NOT NULL";
?>
Can you apply this logic to other criteria?[/quote]
AND products_description.description <> ''
This works, the other one no. I will go back and read more in book to get more familiar with what's happening.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If the IS NOT NULL syntax is not doing what you'd expect, it might be how your fields are set up. If the first one works, use that as it achieves essentially the same thing.
Post Reply