Solved

Mysql table alias as a subquery?

Posted on 2008-06-18
3
1,324 Views
Last Modified: 2013-12-12
Dear experts,

I have an internal and an external employee database
DB: External
TABLE: employee
idemployee  INT
Name VARCHAR(30)

DB: Internal
TABLE: employee
idemployee  INT
Name VARCHAR(30)

When making an autosuggest I perform a simple UNION to search through both.

To store a reponsible of a project it is simple:

TABLE: responsible
idtask INT
idresponsible INT
idemployee  INT
db VARCHAR(30)


To show the values again, I am currently doing the following:

//FETCH DB
$q="SELECT * FROM responsible WHERE idtask='11' ";
$query=mysql_query($q);
if($r = mysql_fetch_array($query))
{
  $db=$r['db'];
  $idempl=$r['idemployee '];
}

//FETCH employee name
$q="SELECT name FROM ".$db.".employee WHERE idemployee='874949'";
$query=mysql_query($q);
if($r = mysql_fetch_array($query))
{
  $name=$r['name'];
}

It works, but I would like to make one single query of the 2 queries above...

I tried:
SELECT * FROM (SELECT CONCAT(db,".","employee")  FROM responsible WHERE idtask=11) as e, responsible as r WHERE e.idemployee=r.idemployee
-> internal.employee
I expected a list of employee names...

Any ideas would be welcome ;)
0
Comment
Question by:ChoobsTech
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21816210
you cannot dynamically join to a database based on a column value...
so you must stick with 2 queries...
0
 
LVL 4

Accepted Solution

by:
albuitra earned 350 total points
ID: 21816250

SELECT * FROM 
(SELECT * from Internal.employee WHERE idtask=11
 union 
select * from External.employee WHERE idtask=11 )  as e

Open in new window

0
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 150 total points
ID: 21821888
or more efficiently.  
using a UNION will be more efficient in this case then a subquery.  (so it is still basically two queries but returns only one result set).
SELECT 'internal' as source, employee.* FROM responsible LEFT JOIN  internal.employee ON employee.idemployee = responsible.idemployee WHERE responsible.taskid = 11
UNION
SELECT 'external' as source, employee.* FROM responsible LEFT JOIN external.employee ON employee.idemployee = responsible.idemployee  WHERE responsible.taskid = 11

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question