Join Question... :)

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
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Join Question... :)

Post by Zoram »

Code: Select all

$query = "
	SELECT r.proId, r.title, r.rating, r.review, p.name, p.pic1
	FROM Review r 
	RIGHT JOIN Products p
		ON r.proId = p.id
	ORDER BY RAND() limit 1;";
Is there something wrong with this? I can't get any results back... And once i do, would i use something like

Code: Select all

$name = $rowї'p.name'];
Or something else?

Thanks
ReDucTor
Forum Commoner
Posts: 90
Joined: Thu Aug 15, 2002 6:13 am

Post by ReDucTor »

it will actually be $row['name'] if you fetch it as an assositive array
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

not sure if this would make any difference?:
A table reference may be aliased using tbl_name AS alias_name or tbl_name alias_name:
mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
-> WHERE t1.name = t2.name;



also (not a bug, but recommended):
RIGHT JOIN works analogously as LEFT JOIN. To keep code portable across databases, it's recommended to use LEFT JOIN instead of RIGHT JOIN.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Not sure what you mean by 'I can't get any results back', does the query not return the results you want or does it just not work at all, ie. show an error?

For error checking make sure that you use mysql_error():

Code: Select all

@$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
Remove the semi-colon ( ; ) after the LIMIT clause as it is not necessary - LIMIT 1 instead of LIMIT 1;.

Mac
Post Reply