RE: [PHP-DB] query from two tables
| From: | Rick Emery | Date: | Mon, 04 Feb 2002 15:03:24 +0000 |
| Subject: | RE: [PHP-DB] query from two tables | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-16487@lists.php.net to get a copy of this message | ||
Since you gave no real details of your table structure, I'll conjecture.
Assume city table and advert table is defined as:
CREATE TABLE city (
city_id int not null, auto_increment,
city_name varchar(50) default ""
)
CREATE TABLE adverts (
ad_id int not null auto_increment,
ad_text varchar(200) default "",
city_id int not null
)
SELECT c.city_name, a.ad_text FROM adverts a LEFT JOIN city c using(city_id)
ORDER BY city;
This will list every advert. Where an advert has no city. the city_name
will display as NULL.
To find cities with no adverts:
SELECT c.city_name, a.ad_text FROM city c LEFT JOIN adverts a using(city_id)
ORDER BY city;
-----Original Message-----
From: Ivan Balazs [mailto:ibalazs@sch.bme.hu]
Sent: Monday, February 04, 2002 6:26 AM
To: php-db@lists.php.net
Subject: [PHP-DB] query from two tables
Hi!
The situation: I have 2 tables in an adverising site. In one table, i have
the cities used by advertisers, and in the other there are the ads.This ad
table has a field which refers to the id of the city in that table.
The problem: what mysql method should i use if i want to order the query
by cities? I have tried multiple selection like select ads.*, cities.*
from ads, cities , but it returns a nonsense.
Any help would be appreciated
Balazs
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php