Jump to content

SQL understanding help?


TheJoey

Recommended Posts

You don't need the aliases.  All the "part a" does is create an alias for part, that you can use rather than having to specify the full table name.

 

Because you only have one table, there is no possible ambiguity, and thus no need for prefixing columns with the table name.

 

That is used when you have multiple tables, in the cases where you are joining them together.

 

 

Link to comment
Share on other sites

Well say i had two tables parts & Customer names

Would i use the alias's then so that i can join the 2 tables?

 

All an alias is -- as the name suggests is an alternative to using the full table name.

 

MySQL supports a few different join syntaxes, so in this case I'll use a simple inner join specified in the WHERE clause to illustrate.

 

SELECT foo.id, foo.name, bar.id, bar.name
FROM foo, bar
WHERE bar.id = foo.bar_id

 

This is exactly the same, only using aliases:

 

SELECT f.id, f.name, b.id, b.name
FROM foo f, bar b
WHERE b.id = f.bar_id

 

Technically, you only need to prefix the table name where there is ambiguity.  In the example, I assume that table foo and bar both have columns named id and name.  This is ambiguous, so if I specified "SELECT id", I'd get an error because mysql doesn't know which id column (foo.id or bar.id) I want.  However, we'll assume that bar_id only exists in table foo, and is a foreign key from the bar table.  That would not be ambigous, so I don't technically need to specify the table prefix of foo, as mysql knows all the columns that exist in the 2 tables.

 

I could thus, write the query this way and it will still work.

 

SELECT f.id, f.name, b.id, b.name
FROM foo f, bar b
WHERE b.id = bar_id

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.