Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthormihai
    • CommentTimeAug 26th 2006
     permalink
    hello guys,
    i've always made my own programing but i've now hit a bit of a problem with mySQL and php.
    i have two tables:
    citys: id_city, city_name
    events: id_event, id_city, event
    how can i select only the cities that have events registered, but i want to have them displayed only once in a list.
    Mike
    • CommentAuthordcrean
    • CommentTimeAug 26th 2006
     permalink
    select distinct city_name from
    citys inner join
    events on events.id_city = citys.id_city

    I can't remember if mysql has a distinct command or not, but I'm sure it does.
    • CommentAuthorvarland
    • CommentTimeAug 26th 2006
     permalink
    I'm not sure if joins or subqueries are more efficient on MySQL, so you could also try:

    SELECT `city_name`
    FROM `citys`
    WHERE `id_city` IN (SELECT DISTINCT `id_city` FROM `events`);
    • CommentAuthordcrean
    • CommentTimeAug 26th 2006
     permalink
    I've usually found that sub queries are less efficient than joins, depending on the tables and the number of joins. for something like this, i'd say a join is better.
    • CommentAuthormihai
    • CommentTimeAug 29th 2006
     permalink
    just wonderful guys, i'm sticking with sub queries for the time beeing :)
Add your comments
    Username Password
  • Format comments as (Help)