1

I have the following PHP code, with MySQL code that I'm having trouble understanding:

$result = $conn->query("SELECT v.*, (select count(*) from $table2 where ParentSyncGuid=v.SyncGuid) children FROM $table2 v WHERE v.ParentSyncGuid IS NULL OR v.ParentSyncGuid = ''");

The $table2 is defined earlier as the correct MySQL table.

However, "v" is not the name of any variable, table, or anything I can understand -- can someone explain the above query? The table being queried has entries where there are a few "root" entries, and every other entry has a parent that eventually links back to the root entries, hence the parent/id syntax, and the above code is meant to find just the root elements from my understanding... but I cannot get it to work and have never seen the "v." syntax before, and cannot find any documentation on it.

3
  • 5
    v is an alias for $table2 Commented Jan 22, 2016 at 21:36
  • 1
    v is an alias..... for $table2 in this case Commented Jan 22, 2016 at 21:36
  • You simply define what you want via tablename.column so you can either specify an existing column name, or use star * to indicate all columns from that table. in your case v is an alias to your tablename table as mentionned by comments Commented Jan 22, 2016 at 21:41

2 Answers 2

5

In the SQL there is this string: FROM $table2 v
The table can be access now with the alias name v

You could use * for selecting all available columns from all joined tables
v.* uses only the columns from table v

Example: Given are the following tables:
TableA with the columns a1 and a2
TableB with the columns a1 and b2

SELECT * FROM TableA JOIN TableB ON (...)

This would give an result set with all 4 columns

SELECT TableA.* FROM  TableA JOIN TableB ON (...)

This would give an result set only with the columns a1 and a2

SELECT v.* FROM  TableA v JOIN TableB ON (...)

This is exactly the same as above, but with alias names

See also: http://www.w3schools.com/sql/sql_alias.asp

Sign up to request clarification or add additional context in comments.

Comments

0

See the "From $table2 v" portion of the SQL statement. That gives the table the alternative name v. This is called an alias.

If it was written fully it would be "From $table2 AS v"

The AS keyword is optional and so is often omitted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.