Page 1 of 1

MySQL query with exchange of reference to IDs

Posted: Thu Jun 07, 2012 7:26 am
by headbull
Hi everyone,

I'm new to this board, and registered to try to solve an issue I have with some SQL queries. I have a database, that has one table for users, one table for shops and another table for services.

When listing the services table, who has references to both the shops and the user table (to their IDs).. I wish to exchange or add fields from the shops and user database. Lets say that my service table looks like this:
id | creator | shopid | comment
I wish for every line it reads from the servicetable, it adds two fields or exchanges creator and shopid with name from the user and the shop table. I guess its possible with the SQL query, but how ?

Hope that someone understand my question, and are able to help me.

Re: MySQL query with exchange of reference to IDs

Posted: Thu Jun 07, 2012 8:21 am
by Celauran
Sounds like you want a JOIN.

Something like this:

Code: Select all

SELECT srv.id, srv.comment, u.name AS user, sh.name AS shop_name
FROM services AS srv
JOIN users AS u ON u.id = srv.creator
JOIN shops AS sh ON sh.id = srv.shopid
WHERE whatever

Re: MySQL query with exchange of reference to IDs

Posted: Thu Jun 07, 2012 8:34 am
by headbull
Do somebody have some easier walkthrough on this usage of JOIN in SQL ?

I'm really struggeling to understand/try to do this.

Did'nt see ur reply before now in regards to the example. I'll work with that, and give u a feedback :)

Re: MySQL query with exchange of reference to IDs

Posted: Sat Jun 09, 2012 8:13 am
by headbull
I managed to solve this! Thank you very much for your help!

I'm new to making the SQL database doing the work for me, but I defintly see how much time you can save on it now and not least reduce the code!