Squadrons
Squadrons

Reputation: 2567

Can someone verify that I'm interpreting this sql statement properly?

I'm using rails and a plugin to manage tags - The query I'm using involves - a tasks table (many to many relationship with users) - a tags table ( stores reference to a tag - id (int), name (string) ) - a taggable table (polymorphic table that references a tag, the taggable item, and the tagger of that item, in my case, tasks)

Here is the sql:

ActsAsTaggableOn::Tag Load (0.1ms)

SELECT "tags".* FROM "tags" WHERE (lower(name) = '#sharedtag')

Task Load (0.4ms)

SELECT "tasks".* 
FROM "tasks" INNER JOIN "task_relationships" 
ON "tasks"."id" = "task_relationships"."task_id" 
JOIN taggings tasks_taggings_f7b47be 
ON tasks_taggings_f7b47be.taggable_id = tasks.id 
AND tasks_taggings_f7b47be.taggable_type = 'Task' 
AND tasks_taggings_f7b47be.tag_id = 23 
WHERE "task_relationships"."user_id" = 1 
ORDER BY tasks.created_at DESC 

What I'm confused about is line 3 of the task load, where tasks_taggings_f7b47be.tag_id shows up out of nowhere. I assume it's some sort of temporary table or reference to a created join table, but have only recently started exploring sql.

Any explanation, links, or general knowledge would be appreciated.

Upvotes: 0

Views: 67

Answers (1)

qbantek
qbantek

Reputation: 705

I think tasks_taggings_f7b47be is an alias to the taggings table => http://www.w3schools.com/sql/sql_alias.asp

It is permissible to omit the AS keyword: "The general syntax of an alias is SELECT * FROM table_name [AS] alias_name. Note that the AS keyword is completely optional and is usually kept for readability purposes." more

Upvotes: 3

Related Questions