I have two tables, a users table and a sources table. In sources, you have any number of records that are linked to the users table by an ID (in this case "emp_id"). This field exists in both tables and is named "emp_id".
So ... what I need is to list out sources either all at once, or by last name, or by owner. In all cases I need to output the owner name from the users table with the source.
So I have some fields that look like this:
users.emp_id
users.f_name
users.l_name
sources.emp_id
sources.f_name
sources.l_name
It's important to note that the f_name/l_name in each table are not the same. In users this is the name of the owner, who may have 1 or 1000 records in the sources table linked to his "emp_id". In essence, it's a simple contact manager, so think of the sources table as a list of contacts that are owned by the person in users table.
What I need to do is output a lists of sources based on a user defined request, you know like show all sources, show my sources, show other people's sources, etc.. What I need is a query that can output sources and then also fetch the owner name from the users table.
What I thought would work okay is something like this:
$sql = "SELECT s.f_name, s.l_name, u.f_name, u.l_name
FROM src_main s INNER JOIN users u ON s.emp_id = u.emp_id" AND s.l_name LIKE '$alphasearch%'
$result = mysql_query($sql);
This does not work. You see in this example I'm trying to say show me all sources that have last names that start with this letter, and also give me the owner f_name and l_name from the users table where the emp_id matches the emp_id in the sources table.
I certainly could do two queries, one where I lookup the sources by l_name letter (or whatever the search criteria happens to be), then loop on the results and do another select inside the loop that looks up the f_name / l_name in users on each trip through the loop - but that's what I want to avoid, it seems I could do this in a much more efficient way.
I wonder if the fact that I have a f_name/l_name in each table is the problem? Since they aren't supposed to match even when emp_id matches does this cause a problem? Is SQL going to assume in a Join that fields witht he same names will always matchup?? Do you see what I'm saying?
I know this may be rambling, but I wanted to provide all the information I could to assist anybody replying.
Thanks in advance!