?
Solved

Mysql table alias as a subquery?

Posted on 2008-06-18
3
Medium Priority
?
1,336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1400 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 600 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

770 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