• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

MySQL Left Join - Appending to variable

I'm joining two tables.  

Let's say table one is...

Table Name: items

Item        ID
Widget    5

Table two:

Table Name: prices

Item Price         URLID
25.00               item/5

My SQL goes like...

SELECT *
FROM items t1
LEFT JOIN prices t2 ON t2.URLID = 'item'.t1.ID
WHERE t1.ID = '5'

What would the proper syntax be to do that?

As you can see - ID's variable in the first table (in this instance 5) relates to the 'item/VARIABLE' in the second table (in this instance, item/5).

The " t2.URLID = 'item'.t1.ID" is apparently wrong syntax - as it doesn't work.

How would I properly phrase that?

Thanks!
0
erzoolander
Asked:
erzoolander
  • 2
  • 2
1 Solution
 
erzoolanderAuthor Commented:
'item/'.t1.ID I mean.  Forgot the slash
0
 
johanntagleCommented:
should just be t1.id. You don't need 'item' since you already aliased it with t1
0
 
erzoolanderAuthor Commented:
Will it match up though, since URLID = item/5 and t1.id = 5?

0
 
johanntagleCommented:
Ah missed that part.  So it should be t1.id/5.

It might cause a slow query though as it might not use an index in t1.id (if any).  Well, if it's just a one-time thing then that's okay.  If you plan to do this regularly, suggest you add another column to items with the values already divided by 5.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now