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
Related
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
I have a database with 22 tables most tables have a few column fields with identical names. When I use INNER JOIN on the tables with the duplicate column names it will ignore them in the last table. Right now I am only joining 3 tables
SELECT * FROM company C
INNER JOIN typeofdealer PI ON C.CompanyID = PI.CompanyID
INNER JOIN typeofservices TS ON PI.CompanyID = TS.CompanyID
So in this example typeofdealer and typeofservices both contain column names Other and OtherText When I get this query back I only see one Other and one OtherText.
I just discovered as I was writing this that if I do the query in "phpmyadmin" that I get it back exactly as intended. I am doing this query using prepared statements in php and outputting the results using:
echo "<PRE>";
print_r($results);
echo "</PRE>";
I believe the problem is that php arrays cannot contain duplicate fields so is there a way to circumvent the issue I am having?
The workaround you can do for this is to use an alias AS to make a distinction between columns with the same names. Something like:
SELECT PI.Other AS Other1, PI.OtherText AS OtherText2, TS.Other AS Other2, TS.OtherText AS OtherText2 FROM company C
INNER JOIN typeofdealer PI ON C.CompanyID = PI.CompanyID
INNER JOIN typeofservices TS ON PI.CompanyID = TS.CompanyID
Actually it will work the same way if you omit the AS keyword say SELECT PI.Other Other1, PI.OtherText OtherText2
The problem is, as you rightfully suggested, that PHP won't allow multiple entries into an array with the same key name.
The simplest way around this, is to alias the fields in the column selection section of the query, like so:
SELECT *, typeofservices.Other as ServicesOther, typeofservices.OtherText as ServicesOtherText FROM company C
INNER JOIN typeofdealer PI ON C.CompanyID = PI.CompanyID
INNER JOIN typeofservices TS ON PI.CompanyID = TS.CompanyID
It's not pretty but it's simple.
What I want to do is to query three separate tables into one row which is identified by a unique reference. I don't really have full understanding of the Join clause as it seems to require some sort of related data from each table.
I know I can go about this the long way round, but can not afford to lose even a little efficiency. Any help would be greatly appreciated.
Table Structure
package_id int(8),
client_id int(8),
unique reference varchar (40)
Each of the tables have essentially the same structure. I just need to know how to query all three, for 1 row.
If you have few tables that are sharing the same or similar definition, you can use union or union all to treat them as one. This query will return rows from each table having requested reference. I've included OriginTable info in case your code will need to refer to original table for update or something else.
select 'TableA' OriginTable,
package_id,
client_id
from TableA
where reference = ?
union all
select 'TableB' OriginTable,
package_id,
client_id
from TableB
where reference = ?
union all
select 'TableC' OriginTable,
package_id,
client_id
from TableC
where reference = ?
You might extend select list with other columns, provided that they have the same data type, or are implicitly convertible to data type from first select.
Let's say you have 3 tables :
table1, table2 and table3 with structure
package_id int(8),
client_id int(8),
unique reference varchar (40)
Let's assume that column reference is unique key.
Then you can use this:
SELECT t1.exists_row ,t2.exists_row ,t3.exists_row FROM
(
(SELECT COUNT(1) as exists_row FROM table1 t1 WHERE
t1.reference = #reference ) t1,
(SELECT COUNT(1) as exists_row FROM table1 t2 WHERE
t2.reference = #reference ) t2,
(SELECT COUNT(1) as exists_row FROM table1 t3 WHERE
t3.reference = #reference ) t3
) a
;
Replace #reference with actual value of unique key
or when you provide output of
SHOW CREATE TABLE
I can rewrite SQL with actual query
It is entirely possible to create a join between tables using a where clause. In fact this is often what I do as I find it leads to clearer information of what you are actually doing, and if you don't get the results you expect you can debug it bit by bit.
That said however a join is certainly a lot quicker to write!
Please bear in mind I'm a bi rusty on SQL so I may have missed remembered, and I'm not going to include any code as you haven't said what DBMS you are using as they all have slightly different code.
The thing to remember is that the join functions on a column with the same data (and type) within it.
It is much easier if each table has the 'joining' field named the same, then it should be a matter of
join on <nameOfField>
However if you wish to use field that have different names in the different tables you will need to list the fully qualified names. ie tableName.FieldName
If you are having trouble with natural, inner and outer, left and right, you need to think of a venn diagram with the natural being the point of commonality between the tables. If you are using only 2 tables inner and outer are equivalent to left and right (with each table being a single circle in the venn diagram) and left and right being the order of the tables in your list in the main part of your select (the first being the left and the second being the right).
When you add a third table this is where you can select any of the cross over section using these keywords.
Again however I have always found it easier to do a primary select and create a temp table, then perform my next join using this temp table (so effectively only need to use natural or left and right again). Again I find this easier to debug.
The best thing is to experiment and see what you get in return. Without a diagram of your tables this is the best I can offer.
in brief...
nested selects where field = (select from table where field = )
and temp tables
are (I think) easier to debug... but do take more writting !
David.
array_of_tables[]; // contain name of each table
foreach(array_of_tables as $val)
{
$query="select * from `$val` where $condition "; // $conditon
$result=mysqli_query($connection,$query);
$result_row[]=mysqli_fetch_assoc($result); // if only one row going to return form each table
//check resulting array ,for your row
}
SELECT * FROM table1 t1 JOIN table2 t2 ON (t2.unique = t1.unique) JOIN table3 t3 ON (t3.unique = t1.unique) WHERE t1.unique = '?';
You could use a JOIN like this, assuming all three tables have the same unique column.
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.
select * from table1 join table2 on table1.column3=table2.column4 where ...
...
$row=mysql_fetch_assoc($result);
However, there are two columns in the new table(table1 join table2) which have the same column name, how to get the values of both respectively?
Call the columsn out specifically with an alias like
SELECT table_1.id as table_1_id, table_2.id as table_2_id
Youll have to list out all or most of the columns form at least one of the tables this way but you can get access to cols with the same name across multiple tables.
Prefix the column name in the select with its table name.
select table1.my_column, table2.my_column
from table1, table2
where table1.id = table2.t1_id
But with this method, you would have to read the columns using their returned order indexes, rather than their names. Another answer mentioned using as to name each column, so consider that if you're going to read them by name.
When there's a column name collision due to a query joining 2+ tables, you can not use *. You can use:
SELECT table_1.*,
table_2.*
FROM table_1,
table_2
If that doesn't return the list of columns you want, you will have to explicitly list every column. There's no way around - it's an all or nothing deal.
Table Aliases
...but typing out the full table name every time can be a pain, which is why you can alias tables:
SELECT t1.*,
t2.*
FROM table_1 AS t1,
table_2 t2
Either alias notation is supported. They're also required if you want to join a table to itself.
Using only the column names in a Select list that you actually need\ is always the best, but when you want everything, then, well, go ahead and use the *:
Select a.*,
b.*,
a.id as a_id,
b.id as b_id,
a.name as a_name,
b.name as b_name
from tablea a,
tableb b
...
It won't hurt to be redundant, as a.* includes a_id and a_name, but there values from the * get lost in the associative array, so just put them back in with new, unique names.