Sql Joining two tables and fetching a value - php

I have two tables 'talent_empcomp' and 'talent_employee_details_v' where i need to fetch a value from 'Emp_Compensation' which is present only in 'talent_empcomp' table but 'Emp_Id' is common in both tables and has same values..i need to fetch 'Emp_Compensation' for a perticular 'Emp_Id'
below is the joint query iam running ..but iam getting an error message "Column 'Emp_Id' in where clause is ambiguous"
SELECT A.*, B.Emp_Compensation
FROM talent_employee_details_v A, talent_empcomp B
WHERE Emp_FirstName like '%' and Emp_Id='$Emp_Id' ORDER BY Emp_FirstName

First, you should use explicit join syntax. Second you should use the table aliases you define:
SELECT A.*, B.Emp_Compensation
FROM talent_employee_details_v A join
talent_empcomp B
on a.EmpId = B.Emp_id
WHERE A.Emp_FirstName like '%' and A.Emp_Id='$Emp_Id'
ORDER BY A.Emp_FirstName
It is a good idea to put aliases before column references so you know where the values are coming from. I am guessing the names are from "A".

Well, you're trying to filter of the Emp_Id column, but it is present in both tables, so the SQL interpreter does not know which column to filter. You need to explicitly qualify the column like B.Emp_Id.
SELECT A.*, B.Emp_Compensation
FROM talent_employee_details_v A, talent_empcomp B
WHERE Emp_FirstName like '%' and B.Emp_Id='$Emp_Id' ORDER BY Emp_FirstName
By the way: Your query might run faster if you remove the like % condition.

SELECT A.*, B.Emp_Compensation
FROM talent_employee_details_v A, talent_empcomp B
WHERE Emp_FirstName like '%' and B.Emp_Id='$Emp_Id' ORDER BY Emp_FirstName

This error occurs when you run a join and have not specified which table to select the column from. All it takes is to specify the table before the column name, like A.Emp_id

Related

How to change column name while joining the query in mysql

I need to change the column name while joining the MySQL query. I am explaining my code below.
select * from grc_action left join grc_users on grc_action.action_owner=grc_users.user_id
I am giving my table below.
grc_action:
id name action_owner
grc_users:
user_id name
The above is my table structure and as both has same column name i.e-name here I need to change the grc_users table column i.e-name while fetching the record. Please help me to solve this problem.
You can use AS
SELECT table1.name AS exampleName, table2.name AS otherName FROM sometable
You can also use this on tables:
SELECT vltn.id, vltn.name FROM veryLongTableName AS vltn
You dont need to type the AS, you can just do SELECT table1.name exampleName to shorten it, but it increases readbility and maintanability to write it, so I recommend doing it with AS.
You may assign different aliases to the name columns from the two different tables.
select
ga.id,
ga.name as action_name,
ga.action_owner,
gu.user_id,
gu.name as user_name
from grc_action ga
left join grc_users gu
on ga.action_owner = gu.user_id;
Note that I also used table aliases which make the query easier to read. In general, doing SELECT * is undesirable, and it is usually better to explicitly list the columns you want.
select *
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
changed query use this query
select *,grc_users.name as grcuser_name,grc_action.name as grcaction_name
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
Here geting two name like this
grcuser_name ,
grcaction_name

SQL query for Selecting from Multiple Tables in single database

I am having 3 tables (c19 , c19b2, g26) in a database
I want to write a SQL Query to search and display all fields of the matched record.
I am using following query:
$query = "SELECT * FROM c19,c19b2,g26 WHERE armyno LIKE '%$searchTerm%'";
But it only works for table c19,
Data from the other 2 tables is not fetched.Each table has a field armyno
Please help me with this
Thank you.
Alright, you are not looking for a JOIN, but a UNION.
SELECT * FROM c19 WHERE armyno LIKE '%$searchTerm%'
UNION
SELECT * FROM c19b2 WHERE armyno LIKE '%$searchTerm%'
UNION
SELECT * FROM g26 WHERE armyno LIKE '%$searchTerm%'
That will let you query all three tables at the same time.
Which DB are you using? This would have worked in SQL Server. However, notice you are doing a cross join of every record to every record... usually you only want to match some records by restriction of a matching key, for example:
select
*
from a
left join b on b.somekey = a.somekey
left join c on c.someotherkey = b.someotherkey
In SQL server you can just say *, but I'm taking it that in your DB engine that didn't work, so try specifying which table. This may in some environments require aliasing as well:
select
a.*,
b.*,
c.*
from tableA as a
left join tableB as b on b.somekey = a.somekey
left join tableC as c on c.someotherkey = b.someotherkey
Generally, you should see the columns from the first table, followed by the columns from the second table, followed by columns from the third table for a given row. If you wanted to get all columns from all tables, but separately, then that would be 3 separate selects.
Lastly, if all 3 tables have "armyno" then I'd expect it to throw an ambiguous field error. In such case you'd want to specify which table's "armyno" field to filter on.

SQL SELECT statement - same column names

Imagine I have the following SELECT statement which has been oversimplified.
SELECT a.Name, b.Name FROM table a LEFT JOIN table b ON a.ID=b.TID
using php I run the following:
while ($result = mysql_fetch_array($results)) {
echo $result["Name"];
}
this will give me the result of b.Name. I am aware I can use a.Name AS aName, B.Name AS bName however this might sometimes complicate things where you have a long query and you use a.*. I tried using $result["a.Name"] but it does not work. I am aware this works $result[0] but again this is not always possible without complicating things.
Is there any other way I can show a.Name please?
simple answer : no.
long answer : the array index at PHP has to be unique. By this, the last similar name column will get the precedence.
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.
source
However, you can solve this by using aliases.
SELECT a.Name as aName, b.Name as bName FROM table a LEFT JOIN table b ON a.ID=b.TID
then you can access the names from both tables by using $result["aName"] and $result["bName"]
Based on your requirements, you could consider dividing your query in to two fetch statements. This would allow you to have the duplicate column names.
SELECT a.* FROM table a LEFT JOIN table b ON a.ID=b.TID
SELECT b.* FROM table b LEFT JOIN table a ON a.ID=b.TID

Get MySQL table.column named records with PHP

For example:
I have the normal column name:
SELECT column FROM....
The way i retrieve it in PHP:
$var = $row["column"];
What i want to know is how to retrieve column records which are named with column and table:
SELECT table.column FROM...
It works the exact same way, it's not going to be prefixed with a table.
so if you do a SELECT table.* FROM ..., and one of the column names is tacos. you can still do $row['tacos'].
Now, if you're doing multiple tables (joins) it can get ambiguous if they have the same names, such as "id". So you can alias them... SELECT table.column as new_name FROM ...
You won't get them returned with table.column. Perhaps use an alias? Could use a delimiter between table / field name part.
SELECT column AS tableName_ColumnName FROM....
It sounds to me like you're trying to select from multiple tables which can be done by using joins:
Lets say you have 3 tables that you need to obtain information from all by a unique ID. You can accomplish this by using a left join:
SELECT a.uniqid, a.testingDate, a.testscore, a.testresults,b.email_addy, b.phone1, c.testlevel as tlevel
FROM Tbl1 a
LEFT JOIN Tbl2 b
ON a.Tbl1 = b.Tbl2
LEFT JOIN Tbl3 c
on a.Tbl1 = c.Tbl3
you can then pull the information just as you always do: $var = $row['testingdate']

Column 'id_f' in where clause is ambiguous

i have a join query using 3 table but i get this problem Column 'id_f' in where clause is ambiguous
$id_f=$_GET['id_f'];
$query="SELECT *, s.name AS van, st.name AS naar, p.titl, p.vname
FROM p1_users, f_confirm AS v
INNER JOIN s_steden AS s ON v.van = s.id
INNER JOIN s_steden AS st ON v.naar = st.id
INNER JOIN p1_users AS p ON v.id_f = p.id_f
AND DATE_FORMAT(date,'%Y-%c-%d')WHERE id_f='$id_f'";
$result=mysql_query($query)or die('Wrong query : ' . mysql_error());
$row=mysql_fetch_assoc($result);
can anyone help?
It means, that two or more tables contain column with name "id_f"(in your case that are p1_users and f_confirm ). You need to specify for which table it related, something like this:
AND DATE_FORMAT(date,'%Y-%c-%d')WHERE p.id_f='$id_f'";
You need to use v.id_f or p.id_f in your where clause as two tables have a column of that name so you need to disambiguate.
It actually doesn't matter which one you use in this case as you are doing an inner join.
This might not be a requirement if you use a natural join but I don't suggest using these.
An ambiguous field is a field in a query that is in two or more tables (or subqueries).
you have two options.
Use table_name prefix on each ambiguous field (table1.field_x, table2.field_x)
Use table alias, and use the alias as prefix on each ambiguous field (alias1.x, alias2.x).
In your example, in the INNER JOIN statement you use the alias for "v.id_f = p.id_f", but then, in the WHERE clause you forget the alias.
The problem is in WHERE clause, after DATE_FORMAT you wrote id_f='$id_f', but you have this column (id_f) in other tables in this query. So the database doesn't know wchich table you really want to use.

Categories