If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > querying, alias is returning NULL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-21-04, 07:05
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
Quote:
Originally Posted by r937
please allow me to suggest that you (1) learn JOIN syntax, and (2) always use indentation and line breaks when writing SQL
Code:
SELECT jos_content.title
     , jos_content.introtext
     , jos_content.created_by
     , jos_content.sectionid
     , jos_content.id
     , jos_users.name
     , jos_sections.title AS 'sectitle'
     , jos_sections.id AS 'secid' 
  FROM jos_content
INNER
  JOIN jos_users
    ON jos_users.id = jos_content.created_by
INNER
  JOIN jos_sections 
    ON jos_sections.id = jos_content.sectionid
Thanks for the tip. You're probably right proper formatting; isn't inner join the same thing as join though?

SQL INNER JOIN Keyword
Reply With Quote
  #2 (permalink)  
Old 08-26-09, 17:18
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
querying, alias is returning NULL

Code:
	$result = mysql_query("SELECT jos_content.title, jos_content.introtext, jos_content.created_by, jos_content.sectionid, jos_content.id, jos_users.name, jos_sections.title AS 'jos_sections.sectitle', jos_sections.id AS 'jos_sections.secid' FROM jos_content, jos_users, jos_sections WHERE (jos_content.created_by = jos_users.id AND jos_content.sectionid = jos_sections.id)");
	$t = 0;
	while($row = mysql_fetch_array($result))
		{
			$entry[$t][0] = $row['id'];
			$entry[$t][1] = $row['title'];
			$entry[$t][2] = $row['name'];
			$entry[$t][3] = $row['sectionid'];
			$entry[$t][4] = $row['introtext'];
			$entry[$t][5] = $row['sectitle'];
			$t++;
		}
for some reason $row['sectitle'] is returning null. Any ideas as to what would cause this? It's only that one attribute.
Reply With Quote
  #3 (permalink)  
Old 08-26-09, 17:21
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
sorry, I cleaned it up a little bit :P

Code:
$query = "SELECT jos_content.title, jos_content.introtext, jos_content.created_by, ".
	"jos_content.sectionid, jos_content.id, jos_users.name, jos_sections.title AS 'jos_sections.sectitle', ".
	"jos_sections.id AS 'jos_sections.secid' FROM jos_content, jos_users, jos_sections ".
	"WHERE (jos_content.created_by = jos_users.id AND jos_content.sectionid = jos_sections.id)";
	$result = mysql_query($query);
	$t = 0;
	while($row = mysql_fetch_array($result))
		{
			$entry[$t][0] = $row['id'];
			$entry[$t][1] = $row['title'];
			$entry[$t][2] = $row['name'];
			$entry[$t][3] = $row['sectionid'];
			$entry[$t][4] = $row['introtext'];
			$entry[$t][5] = $row['sectitle'];
			$t++;
		}
still no luck though
Reply With Quote
  #4 (permalink)  
Old 08-26-09, 17:26
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
bangs head against mysql manual. lol, ok I was using the alias incorrectly.

Code:
$query = "SELECT jos_content.title, jos_content.introtext, jos_content.created_by, ".
	"jos_content.sectionid, jos_content.id, jos_users.name, jos_sections.title AS 'sectitle', ".
	"jos_sections.id AS 'secid' FROM jos_content, jos_users, jos_sections ".
	"WHERE (jos_content.created_by = jos_users.id AND jos_content.sectionid = jos_sections.id)";
Reply With Quote
  #5 (permalink)  
Old 08-26-09, 20:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
please allow me to suggest that you (1) learn JOIN syntax, and (2) always use indentation and line breaks when writing SQL
Code:
SELECT jos_content.title
     , jos_content.introtext
     , jos_content.created_by
     , jos_content.sectionid
     , jos_content.id
     , jos_users.name
     , jos_sections.title AS 'sectitle'
     , jos_sections.id AS 'secid' 
  FROM jos_content
INNER
  JOIN jos_users
    ON jos_users.id = jos_content.created_by
INNER
  JOIN jos_sections 
    ON jos_sections.id = jos_content.sectionid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 08-27-09, 10:20
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
Quote:
Originally Posted by r937
please allow me to suggest that you (1) learn JOIN syntax, and (2) always use indentation and line breaks when writing SQL
Code:
SELECT jos_content.title
     , jos_content.introtext
     , jos_content.created_by
     , jos_content.sectionid
     , jos_content.id
     , jos_users.name
     , jos_sections.title AS 'sectitle'
     , jos_sections.id AS 'secid' 
  FROM jos_content
INNER
  JOIN jos_users
    ON jos_users.id = jos_content.created_by
INNER
  JOIN jos_sections 
    ON jos_sections.id = jos_content.sectionid

Thanks for the tip. My code can definitely use some formatting, I was under the assumption that inner join and join where the same though though.

SQL INNER JOIN Keyword
Reply With Quote
  #7 (permalink)  
Old 08-27-09, 13:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by Schweppesale
I was under the assumption that inner join and join where the same though though.
yes, sort of

first, JOIN equates to INNER JOIN, at least in mysql, but it's better always to write INNER JOIN and not just JOIN (in earlier versions JOIN equated to CROSS JOIN)

second, you had FROM jos_content, jos_users, jos_sections WHERE ... which technically is an inner join but this syntax is old style and should be rewritten with JOIN syntax

besides, if you wanted to change one of those joins to an outer join, you'd have to rewrite it anyway
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 08-27-09, 13:13
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by r937
JOIN equates to INNER JOIN
That's what the standard requires.
Quote:
Originally Posted by ANSI SQL
If a <qualified join> or <natural join> is specified and a <join type> is not specified, then INNER is implicit.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On