0

I have a table in sql, I use this statement to make a select:

SELECT id, name, genre FROM table

Now genre is id (int) and I have table called Genres and there I have :

id (int)
name (string)

What select will give me in genre the name and not the id.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
YosiFZ
  • 7,466
  • 20
  • 105
  • 206
  • 2
    What are you trying to do? What SQL do you have so far, and what results does that give you? – Mark Byers Apr 22 '12 at 10:03
  • I'm hesitating whether what I imagine should be entered as an answer or not. It's certainly unclear what you want to ask. – Lion Apr 22 '12 at 10:08
  • Oh well some questions: What DB are you querying? What code did you use to execute the query? Can you already write a query on the DB console that gives you the desired result? – Alessandro Rossi Apr 22 '12 at 10:09
  • What's the relation of your question with C#? – Alessandro Rossi Apr 22 '12 at 10:15
  • **SQL** is just the Structured Query Language - a language used by many database systems - SQL is **NOT** a database product... stuff like this is very often vendor-specific - so we really need to know what database system you're using.... – marc_s Apr 22 '12 at 10:17

3 Answers3

2
SELECT t.id, t.name, g.name 
FROM table t 
   JOIN genres g ON t.genre = g.id
McGarnagle
  • 98,751
  • 30
  • 222
  • 258
Manatherin
  • 4,149
  • 5
  • 35
  • 51
1
SELECT t.id, t.name, g.name
FROM table t, genres g
WHERE t.genre = g.id
duncanportelli
  • 3,131
  • 7
  • 36
  • 59
  • This does the same as [the accepted answer](http://stackoverflow.com/a/10267007/578288), but is far slower. In general, `JOIN`s are faster than a Cartesian Product and a `WHERE`. At least, I learned that in [Database Systems class](https://www.cs.drexel.edu/~jsalvage/Winter2012/CS461/index.html), but I don't remember the explanation behind it. – Rory O'Kane Apr 22 '12 at 23:38
  • 2
    @RoryO'Kane No, it doesn't make any difference. See: http://stackoverflow.com/questions/121631/inner-join-vs-where – duncanportelli Apr 23 '12 at 13:31
  • 1
    You're right. I asked my professor about that, and he said he didn't mean that `WHERE` was slower. It was just for teaching purposes that he wanted us to avoid `WHERE` syntax, because it can only be used to do an *inner* join. He wanted us to learn the syntax for `INNER JOIN` instead so that it would be easier to later teach us `OUTER JOIN`, `LEFT JOIN`, etc., which have the same syntax. – Rory O'Kane May 11 '12 at 01:01
0

You need to use Join

 select G.name 
    from table table1 T,Genres G
    where T.genre = G.id;
Habib
  • 212,447
  • 27
  • 392
  • 421