MySQL not showing duplicate column names in separate tables using INNER JOIN - php

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.

Related

join of two sql tables

I have two tables and I want to join them to get the desired output.
Say the 1st table (seat1) is
and the 2nd table (collegestudents) is
The desired output is
I have tried the below code. But it fails to give the desired result.
$rde2=mysqli_query($con, "select * from seat1 s
left JOIN collegestudents c ON c.Roll = s.Roll
");
Any help please.
You want a left join. Your query looks fine, but you would need not to use select *, and instead explictly list the columns that you want to select, using table prefixes. Otherwise, since you have a Roll column in both tables, a name clashes will happen, that your application apparently does not handle well.
select
s.Roll,
c.Name,
s.Subject
from seat1 s
left join collegestudents c on c.Roll = s.Roll

Excess colomns in the query result

I'm trying to write a simple interface for a list of companies using MySQL and PHP. So, I want to fetch some information from my database.
Here are my tables:
companies_data - only for system information.
corporate_data - here I want to keep information about big companies.
individual_data - and here I want to keep information about little companies.
So, here is the tables
And here is the query that I've written:
SELECT
a.id,
a.user_id,
a.added,
a.`status`,
a.company_id,
a.company_type,
a.deposit,
a.individual_operations_cache,
a.corporate_operations_cache,
a.physical_operations_cache,
b.full_name,
b.tax_number,
b.address,
b.statement_date,
b.psrn,
c.full_name,
c.tax_number,
c.address,
c.statement_date,
c.psrn
FROM
companies_data a
LEFT OUTER JOIN corporate_data b
ON (a.company_id = b.id) AND a.company_type = 0
LEFT OUTER JOIN individual_data c
ON (a.company_id = c.id) AND a.company_type = 1
WHERE
a.user_id = 3
This is just the code for a test, I'll expand it soon.
As you see, I've got result with extra fields like %field_name%1, %another_field_name%1 and so on. Of course it is not the mysql error - what I've asked that I've got - but I want to remove this fields? It's possible or I must convert this output on the application side?
thos %field_name%1, %another_field_name%1 , are visible since you are selecting them in your query:
b.full_name,
b.tax_number,
b.address,
b.statement_date,
b.psrn,
c.full_name,
c.tax_number,
c.address,
c.statement_date,
c.psrn
When you use fields with the same name in distinct tables, then the result column name come with this identifier field1, field2, fieldn... in order to distinguish from which table does the field come from.
If you want to avoid this names, you can use aliases as follows:
[...]
b.full_name as corporate_full_name,
[...]
Probably, if every common fields are coincident, you won´t need to show them all, so just remove them from the select.
Hope being usefull for you.
Br.

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

SUM value from query changes when i add inner join to the query

$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments
FROM totals
INNER JOIN users
GROUP BY totals.idseller;");
When i add the INNER JOIN the sum value is changed. Why?
In my SQL table i have one record in totals width this value: 8943.09 but when i do the some the result is giving me this value: 44715.45
What i am doing wrong?
$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments FROM totals
INNER JOIN users ON totals.idseller = users.idseller
GROUP BY users.UserName;");
Use this Hope this will help you.
When you INNER JOIN to another table, the returned data set is modified to only include rows that exist in both tables. In this case it is likely that there are rows in 'totals' that do not have a matching row in users - either the totals.idseller field might accept null values, or data has become orphaned when matching users have been deleted or edited.
If you want all data in 'totals' regardless of matching user you would user a LEFT JOIN instead in ms-sql, I suspect a similar approach will work in my-sql
You should give an "on" based on the ids. Such as like
inner join users on users.id = totals.idseller
Otherways the sql server will combine all possible rows in the tables, which is most cases not what you wish.
Because when you are adding inner join in your SQL Query, it means you are selecting the data which is common in both the tables.
EX:
SELECT * FROM TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.ID = TABLE_B.ID
If you are joining users table which contains 5 records. By joining table, as there is no any column mapping, this sum-up 5 times and this is reason for showing different values.
Please let me know something wrong in it.
Thanks,
Umehs

Join mysql with multiple queries

I have three different SQL tables I need to join:
table "internet" with columns id|type|status
table "type_list" with columns id|type_name
table "status_list" with columns id|status_name
I want to output text from the two other tables (type_list, status_list) but not values as numbers which currently I have in table "internet".
I also don't want to make lazy programming - PHP array to make ID's equal to something like
$type_list = array("1"=>"VDSL2","2"=>"ADSL");
$status_list = array("1"=>"Pending","2"=>"Active");
because the text is already in the tables, i just dont know how to join them and output the text as query combined together in one query.
Use JOIN
SELECT i.id, type_name, status_name
FROM internet i
LEFT OUTER JOIN type_list t ON t.id = i.type
LEFT OUTER JOIN status_list s ON s.id= i.status
Read the MySQL doc for more informations.
Just write the select with the fields you want.
select internet.id,type_name,status_name from internet
inner join type_list
on type_list.id=internet.id
inner join status_list
on status_list.id=internet.id
For this you need a LEFT JOIN, like so:
SELECT i.id, t.type_name, s.status_name
FROM internet AS i
LEFT JOIN type_list AS t ON t.id = i.id
LEFT JOIN status_list AS s ON s.id= i.id
From your question, it is unclear what field you would like to join the queries on. In the above example, the queries are joined on the id field.
Please also note that the AS is not actually necessary, I have just put it in there to make it clear what is going on

Categories