SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to create a view from three tables

    I have problem with getting combined records from 3 tables.

    Here is the structure of the tables:

    HTML Code:
    CREATE TABLE IF NOT EXISTS `adds` (
    `addid` int(11) NOT NULL AUTO_INCREMENT,
    `addtypeid` varchar(45) NOT NULL,
    `addcreatedon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `addtitle` varchar(255) DEFAULT NULL,
    `addtext` text NOT NULL,
    PRIMARY KEY (`addid`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=40 ;
    
    
    CREATE TABLE IF NOT EXISTS `adds_filters` (
    `addfilterid` int(11) NOT NULL AUTO_INCREMENT,
    `addid` int(11) NOT NULL,
    `filterid` int(11) NOT NULL,
    PRIMARY KEY (`addfilterid`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=40 ;
    
    
    CREATE TABLE IF NOT EXISTS `categories_filters` (
    `filterid` int(11) NOT NULL AUTO_INCREMENT,
    `catid` varchar(45) NOT NULL,
    `filtername` varchar(45) NOT NULL,
    `sorder` int(11) DEFAULT NULL,
    `visible` int(11) DEFAULT NULL,
    PRIMARY KEY (`filterid`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=230 ;
    
    Adds have one to many relationship with adds_filters. That is, one add can have more then one filter.

    What I need is the following:

    I would like to create a view which when select * would return all rows from adds, together with filterid(s) and respective filtername(s). Please note that one add may have many filterid(s)

    On the end, just to mention that I make numerous attempts using different kind of joins, without any luck. I would prefer view solution over the join, because within the view, i will easly make select * instead of creating complicated joins, and more over, there might be queries which may not have any filters.

    Can anyone help me with this?

    Regards, zoreli

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by zoreli View Post
    I would prefer view solution over the join
    the view solution ~is~ joins

    you have to get the joins working first, before you can create the view

    what columns would you like your dreaded, evil "select star" to return?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •