GetDotted Domains

Viewing Thread:
"Multiple MySQL Inner Join with Table Aliases"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Mon 25/07/05 at 14:14
Regular
"NULL"
Posts: 1,384
Here's a bit of a tricky one. I'm not very good with joins, and this is proving nigh on impossible.

I have two tables, one called `content` and one called `pages`.

`content` has 2 columns - `contentid`, `text`, `languageid`
`pages` has 4 columns = `pageid`, `name`, `title`, `body`

Each of the columns in the `pages` table is an integer, and the values in `name`, `title`, `body` link to the `contentid` column in `content`.

For example, I might have the following data in a row in `pages`:

'1', '1', '2', '3'

and the following rows in `content:

'1', 'Test Name', '1'
'2', 'Test Title, '1'
'3', 'Test Body', '1'

If I do a SELECT * query on `pages`, I get the integers, what I want returned is something like:

'1', 'Test Name', 'Test Title', 'Test Body'

where I can specify the `content`.`languageid` and `pages`.`pageid` to match. Anybody got any ideas how to do it? My best shot so far has been:

SELECT `pages`.`pageid`, `content1`.`text` AS `title` FROM ( `content` AS `content1` INNER JOIN `pages` ON (`content1`.`contentid`=`pages`.`title`)) WHERE `pages`.`pageid`='1' AND `content1`.`languageid`='1'

This returns:

'1', 'Test Title'

But I can't work out how to expand it to return the rest. Please help!
Mon 25/07/05 at 15:30
Regular
"NULL"
Posts: 1,384
IGNORE :: Nimco = stupid! Hadn't actually bothered calling the query in PHP....

Hmmmmm, my query is now:

SELECT `pages`.`pageid`, `pages`.`block`,
`namedata`.`value` AS `name`,
`titledata`.`value` AS `title`,
`bodydata`.`value`
FROM `pages`
INNER JOIN `content` AS `namedata` ON `namedata`.`contentid`=`pages`.`name` INNER JOIN `content` AS `titledata` ON `titledata`.`contentid`=`pages`.`title`
INNER JOIN `content` AS `bodydata` ON `bodydata`.`contentid`=`pages`.`body`
WHERE `pages`.`pageid`='1' AND `namedata`.`languageid`='1' AND `titledata`.`languageid`='1' AND `bodydata`.`languageid`='1'

Works fine in phpMyAdmin, but PHP itself won't have it. Hmmm. Says invalid resource.
Mon 25/07/05 at 15:21
Regular
"NULL"
Posts: 1,384
Perfect, you God! Lol. There shouldn't be a problem with a clash cos I use backticks, but I'll change the fieldname anyway.
Mon 25/07/05 at 15:05
Regular
"Devil in disguise"
Posts: 3,151
Several ways you can do it, but I guess the method you're trying to do is...

SELECT pages.pageid,namedata.ctext,titledata.ctext,bodydata.ctext from pages
INNER JOIN content AS namedata ON namedata.contentid=pages.name
INNER JOIN content AS titledata ON titledata.contentid=pages.title
INNER JOIN content AS bodydata ON bodydata.contentid=pages.body

(note: I used ctext instead of text as a field name because of the clash with the datatype)
Mon 25/07/05 at 14:14
Regular
"NULL"
Posts: 1,384
Here's a bit of a tricky one. I'm not very good with joins, and this is proving nigh on impossible.

I have two tables, one called `content` and one called `pages`.

`content` has 2 columns - `contentid`, `text`, `languageid`
`pages` has 4 columns = `pageid`, `name`, `title`, `body`

Each of the columns in the `pages` table is an integer, and the values in `name`, `title`, `body` link to the `contentid` column in `content`.

For example, I might have the following data in a row in `pages`:

'1', '1', '2', '3'

and the following rows in `content:

'1', 'Test Name', '1'
'2', 'Test Title, '1'
'3', 'Test Body', '1'

If I do a SELECT * query on `pages`, I get the integers, what I want returned is something like:

'1', 'Test Name', 'Test Title', 'Test Body'

where I can specify the `content`.`languageid` and `pages`.`pageid` to match. Anybody got any ideas how to do it? My best shot so far has been:

SELECT `pages`.`pageid`, `content1`.`text` AS `title` FROM ( `content` AS `content1` INNER JOIN `pages` ON (`content1`.`contentid`=`pages`.`title`)) WHERE `pages`.`pageid`='1' AND `content1`.`languageid`='1'

This returns:

'1', 'Test Title'

But I can't work out how to expand it to return the rest. Please help!

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Continue this excellent work...
Brilliant! As usual the careful and intuitive production that Freeola puts into everything it sets out to do, I am delighted.
Second to none...
So far the services you provide are second to none. Keep up the good work.
Andy

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.