Stefano Cerelli
Stefano Cerelli

Reputation: 105

Can't understand this MySQL Query Purpose

I'm doing an internship as Web Developer, I'm a bit new and i'm here to ask your experience to clarify my doubts; while i was working on a website that someone else made i found this query:

$query = "SELECT a.upload_date, a.zipfile, a.lang, b.*
          FROM something_uploadform as a , something2_stayinformed as b
          WHERE a.uid_head =b.uid and a.zipfile<>'' order by ".$orderby." desc";

Can anyone help me to understand it? I thought that this piece of code picks a record and attach it to an a, isn't it?

Thanks everyone.

Upvotes: 1

Views: 91

Answers (2)

Kermit
Kermit

Reputation: 34055

At a high level, this query is doing an implicit JOIN on two tables. Let's break it down:

The data is coming from two tables (AS gives the table an alias):

  • something_uploadform as "a" (this table will now be known as a)
  • something2_stayinformed as "b" (this table will now be known as b)

The columns being selected:

  • a.upload_date
  • a.zipfile
  • a.lang
  • b.* (All columns in table b)

The tables are being joined on the columns:

  • a.uid_head = b.uid

The filter being applied:

  • a.zipfile <> '' (where the column zipfile is not empty)

The sort being applied:

  • $orderby DESC (passed variable, sorted in descending order)

Upvotes: 4

pritaeas
pritaeas

Reputation: 2093

something_uploadform as a defines a as an alias for something_uploadform so you don't have to specify the full table name when selecting columns.

Upvotes: 1

Related Questions