PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

[FIXED] How do you join two fields in one table into a second table?

 March 15, 2022     lamp, mysql     No comments   

Issue

I have a table which contains three fields two of which link into the same table to lookup the value (the third links to a second table)

Deployment Table
---------------------------------
INDEX,  APP,  DEVSERVER, PRODSERVER (was previously just SERVER)

Server Table
---------------------------------
INDEX, NAME

App Table
---------------------------------
INDEX, NAME

Previously, when I had just SERVER in the deployment table I was doing:

SELECT d.index, a.name as `app`, s.name as `server`
FROM deployment as `d`, app as `a`, server as `s`
WHERE d.app = a.index AND d.server = s.index

and it was working fine. But now that I want to "double dip" into the server table with DEVSERVER and PRODSERVER I've not the foggiest way how to go about it.


Solution

First, you should use proper join syntax. Second, you need to introduce a second join to the server table:

SELECT d.index, a.name as app, dserv.name as devserver, pserv.name as prodserver
FROM deployment d join
     app a
     on d.app = a.index join
     server dserv
     on d.devserver = dserv.index join
     server pserv
     on d.prodserver = pserv.index;


Answered By - Gordon Linoff
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing