Excess colomns in the query result - php

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.

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

Why my PHP MYSQL query not working/running after i enter this query?

I have two tables tableOne = 90K data and tableTwo = 100k data, i will look for the duplicate numbers on both tables with the given conditions and the matching must be 1:1 if multiple match are on the other table only one will be tagged as match (given that the data on both tables has match data).
I have this select statement below, but when i run it on my local xampp and even on CMD the screen freezes after i press enter then it takes hours before it returns an error out of memory. Hope you can help me with this.
SELECT rNum,
cDate,
cTime,
aNumber,
bNumber,
duration,
tag,
aNumber2,
bNumber2,
'hasMatch',
concatDate,
timeMinutes
FROM tableOne a
LEFT JOIN
tableTwo b ON a.aNumber2 = b.aNumber2
AND a.bNumber2 = b.bNumber2
WHERE a.hasMatch = 'valid'
AND (a.duration - b.duration) <= 3
AND (a.duration - b.duration) >= -3
AND TIMEDIFF(a.concatDate,b.concatDate) <= 3
AND TIMEDIFF(a.concatDate,b.concatDate) >= -3
Thank you In advance.
If you're doing 1:1 relationship with two tables then I think you should probably go with INNER JOIN rather than LEFT JOIN
Secondly, your query doesn't seem to be indexed properly. So, better would be using EXPLAIN SELECT ... to see the profile of SQL and create INDEXES for Filters.
in your SELECT you have aNumber2 and based on your join rule both table a and table b have aNumber2 column. it's a problem. if two table have a column with the same name, on select you should specify the table.
for example like this
SELECT a.aNumber2 as a_number2,....
in your query the same problem exists for other columns like duration and concatDate
another thing is you should use INNER JOIN in your case instead of LEFT JOIN.
if you final result have many rows(thousands), take them step by step... add LIMIT to your example and take 100 result each time.

Creating a completion table (PHP and MYSQL)

My database has a challenges table where there are these columns: Challenge_Name, Challenge_Description. I have a 2nd table called completed_challenges_junction and it has these columns: Member_Name, Challenge_Name.
I need a way to display all of the challenge names from the challenges table along with the member names within the completed_challenges_junction. If there is no match then I would like it to display NULL.
I think I'm pretty close to having my SQL code working, here is what I have now.
SELECT challenges.Challenge_Name, challenges.Challenge_Description, completed_challenges_junction.Member_Names
FROM challenges
LEFT JOIN completed_challenges_junction ON challenges.Challenge_Name=completed_challenges_junction.Challenge_Name
This works but also bring duplicate entries of another member. If i use WHERE Member_Name='testmember' it only brings the entries of the member when I need it to still display all Challenge_Names.
SELECT
A.Challenge_Name,A.Challenge_Description,
GROUP_CONCAT(IFNULL(B.Member_Names,'')) Member_Names
FROM
challenges A
LEFT JOIN completed_challenges_junction B ON
A.Challenge_Name=B.Challenge_Name AND
A.Challenge_Description=B.Challenge_Description
GROUP BY
A.Challenge_Name,A.Challenge_Description
;
or
SELECT
A.Challenge_Name,A.Challenge_Description,
GROUP_CONCAT(IFNULL(B.Member_Names,'')) Member_Names
FROM
challenges A
LEFT JOIN completed_challenges_junction B
USING (Challenge_Name,Challenge_Description)
GROUP BY
A.Challenge_Name,A.Challenge_Description
;
You could add the WHERE clause into the ON condition like this
SELECT challenges.Challenge_Name, challenges.Challenge_Description, completed_challenges_junction.Member_Names
FROM challenges
LEFT JOIN completed_challenges_junction ON (
challenges.Challenge_Name=completed_challenges_junction.Challenge_Name
AND Member_Name='testmember'
)
This way only testmember entries will come up from completed_challanges_junction, but all challenges will be displayed.
I'm not sure what exactly you want, but maybe you are looking for 'group by' clause and group_concat function:
SELECT challenges.Challenge_Name, challenges.Challenge_Description,
GROUP_CONCAT(completed_challenges_junction.Member_Names SEPARATOR ', ')
FROM challenges
LEFT JOIN completed_challenges_junction
ON challenges.Challenge_Name=completed_challenges_junction.Challenge_Name
GROUP BY challenges.Challenge_Name
This query will return result where each Challenge_Name occurs only once.

how to retrieve data without using outer joins, or without using any joins?

How can we retrieving data from multiple tables without using "LEFT JOIN" or "RIGHT JOIN" in mysql?
OR
Is it Possible by using PHP/Mysql?
some of the case i can retrive data. such as when we use "two tables" or there have no complex "WHERE" condition.
But when i use 5,6,7----15 tables that case it is impossible for me to retrive data.
Please can any one help me.
Thanks Advance.
Do a search on the first table,
On the second table do a SELECT * FROM table_2 WHERE table_2.fk_table_1 IN ($pks_from_table_one)
and go on ... but this means you will do n queries, with n round trips to the DB - I've heard that some companies forbids JOINs and sometimes it is indeed better - but I didn't do it yet and don't recommend it either.
Let's say you have foods and people. You want to get the foods people like.
foods: id,food_name
people: id,name,food_id
food_id here refers to the id field in the foods table.
Simply do:
SELECT people.name,food.food_name FROM people,foods WHERE people.food_id=foods.id
You can use "alias" instead of "Join".
Assume you have 4 table.
Syntax:
Select * from Table1 A, Table2 B, Table3 C, Table4 D
where A.id= B.id and A.city=C.city and B.subject=D.subject
You can make this by adding a subquery without using any JOIN statement, in the example I gave you three tables were used to extract Users that are our customers and their mount is less or equal than 999, check it out..
Select TUsers.names TCustomers.names
FROM TUsers, TCustomers
Where TUsers.id = TCustomer.id AND TCustomers.id IN
(Select TCustomer.id
FROM TCustomer
WHERE TCustomer.amount >= 999)
Rate if it helps :)

Having trouble with SQL command logic - please help

I am using the following statement to fill an Excel spreadsheet with student information, including "actualstudenthours."
The problem is, I want to show all students for which the tblstudentstatus.id = 3, but I also need to show actual student hours for those students. Unfortunately, not all of the students have a corresponding entry in "viewactualstudenthours." This statement completely leaves out those students for which there is no corresponding entry in "viewatualstudenthours."
How do I get all the students to show up where the tblstudentstatus.id = 3?
If there is no entry for them in viewactualstudenthours, it should not omit the student entirely...the student hours fields should just be blank. Your help would be greatly appreciated.
$result=mysqli_query($dbc,"SELECT tblstudent.first, tblstudent.last,
LEFT(viewactualstudenthours.ACTUAL_remain,5),
(SELECT first from tbladdress where tbladdress.id = tblstudent.contact2),
(SELECT last from tbladdress where tbladdress.id = tblstudent.contact2),
tbladdress.address1,tbladdress.city,tbladdress.state,tbladdress.zip1,
tbladdress.phone, tbladdress.cell, tbladdress.email
FROM tblstudent, tbladdress, tblstudentstatus, viewactualstudenthours
WHERE viewactualstudenthours.student_id = tblstudent.id
AND tblstudent.status = tblstudentstatus.id
AND tbladdress.id = tblstudent.contact1
AND tblstudentstatus.id = 3");
(Note: the editor made the SQL semi-legible - but probably broke every rule in the PHP code book.)
Learn to use the explicit join notation introduced in SQL-92 instead of the older comma-separated list of table names in the FROM clause.
You need to use a LEFT OUTER JOIN of the table tblstudent with the view viewactualstudenthours. Ignoring the quotes etc needed to make the code work in PHP, you need:
SELECT S.first, S.last,
H.ACTUAL_remain,
A2.first, A2.last,
A1.address1, A1.city, A1.state, A1.zip1,
A1.phone, A1.cell, A1.email
FROM tblstudent AS S
JOIN tbladdress AS A1 ON S.Contact1 = A1.ID
JOIN tbladdress AS A2 ON S.Contact2 = A2.ID
JOIN tblstudentstatus AS T ON S.Status = T.ID
LEFT JOIN viewactualstudenthours AS H ON S.ID = H.Student_ID
WHERE T.id = 3
Also learn to use table aliases (the AS clauses) - it simplifies and clarifies the SQL. And if the schema is up to you, don't prefix the table names with 'tbl' and the view names with 'view' - it is just so much clutter.
Note that I got rid of the sub-selects in the select-list by joining to the Address table twice, with two separate aliases. I removed the function LEFT(); you can reintroduce it if you need to.

Categories