Currently I am looking to refactor my code in php to use Active Record or DataMapper. I have a complex sql query where one table is the base and to it I join multiple tables in several levels, ie, join a table to which another table is joined.
For example:
Select <list of columns>
from main_table
left join table1
left join table2 as t1
left join table2 as t2
left join table3
left join table4 on table3.key = table4.key
left join ( select * from table5 ) as t3
left join ( select * from table6 where id in ( select id from table7 ) ) as t4
where <conditions>
What is better to use? Active Record or DataMapper.
Also I want it to be handy to uptade or insert to the main table. And also do some show/output functions. Handle input from user.
Any advise is welcome.
I havew for example 'employees' table and 'phones' tables , now i select them together
SELECT employees.*, phones.*
FROM employees JOIN
phones
ON employees.id = phones.id
after the fetch:
now the question is how to show it in table by using PHP foreach loop maybe,
no matter while there are 'employees' that have few phones at the phones table
and thats mean that there are few phones with the same id,
how do i combine all the phones with the same id that equal the employees id to one column ?
you can use any option
SELECT employees., phones.
FROM employees INNER JOIN phones ON employees.id = phones.employe_id
SELECT employees., phones.
FROM employees LEFT JOIN phones ON employees.id = phones.employe_id
SELECT employees., phones.
FROM employees OUTER JOIN phones ON employees.id = phones.employe_id
Please make your tables are design in a right way
There should be always primary key on Tables and second table should have foreign key to join
and we have different join
inner join
outter left join
Outter right join
[https://www.w3schools.com/sql/sql_foreignkey.asp][1]
I have written a query using inner join which consist of multiple tables
SELECT *
FROM admin_info
INNER JOIN admin_login
INNER JOIN gender
INNER JOIN admin_type
INNER JOIN area
INNER JOIN document_type
INNER JOIN permissions
ON admin_login.admin_id=admin_info.admin_id
AND admin_type.admin_type_id=admin_info.admin_type_id
AND area.area_id=admin_info.area_id
AND document_type.document_id=admin_info.document_id
AND permissions.permission_id=admin_info.permission_id
The above query works but gives multiple results of the same record or duplicate records
Please help me fix the query or provide with an alternative to this query
also please suggest me ways to optimize the query for faster processing??
The above Query
SELECT *
FROM admin_info
INNER JOIN admin_login
INNER JOIN gender
INNER JOIN admin_type
INNER JOIN area
INNER JOIN document_type
INNER JOIN permissions
ON admin_login.admin_id=admin_info.admin_id
AND admin_type.admin_type_id=admin_info.admin_type_id
AND area.area_id=admin_info.area_id
AND document_type.document_id=admin_info.document_id
AND permissions.permission_id=admin_info.permission_id
Below Given is the screen shot of the results.
I want only one record per person and not duplicate records?
Apart from that when i use foreach/while loop the results vary please help?
Actually The problem was with gender table which had no reference in the query as soon as i removed the gender table inner join it worked as expected!
I have a table for comments ("event_comments") to different events with the following columns:
post_id
event_id
username
comment
date
I want to be able to retrieve this info from the database and also be able to print the username, first name and last name; for this, I thought of using INNER JOIN, but it is not working for the following reason: I have 3 different profile types (3 different tables) "students", "guardians", "teachers" and when I try to use the INNER JOIN using "username" I get an error message saying that Column 'username' in from clause is ambiguous.
SELECT event_comments.post_id, event_comments.event_id, event_comments.username, event_comments.comment, event_comments.date,
students.first_name, students.last_name, students.picture,
guardians.first_name, guardians.last_name, guardians.picture,
teachers.first_name, teachers.last_name, teachers.picture
FROM event_comments
INNER JOIN students
INNER JOIN guardians
INNER JOIN teachers
USING (username)
ORDER BY date DESC
LIMIT 20
I tried to do this and it worked, but it only shows 1 comment per user; if the user has more than 1 comment then the info is ignored:
SELECT event_comments.post_id, event_comments.event_id, event_comments.username, event_comments.comment, event_comments.date,
students.first_name, students.last_name, students.picture,
guardians.first_name, guardians.last_name, guardians.picture,
teachers.first_name, teachers.last_name, teachers.picture
FROM event_comments
INNER JOIN students
INNER JOIN guardians
INNER JOIN teachers
GROUP BY username
ORDER BY date DESC
LIMIT 20
Does anybody how to get the INNER JOINs to work? is there a better way to do what I want? I hope I explained myself well.
Thanks!
do it like this:
SELECT event_comments.post_id, event_comments.event_id, event_comments.username, event_comments.comment, event_comments.date,
students.first_name, students.last_name, students.picture,
guardians.first_name, guardians.last_name, guardians.picture,
teachers.first_name, teachers.last_name, teachers.picture
FROM event_comments
INNER JOIN students
on event_comments.username=students.username
INNER JOIN guardians
on event_comments.username=guardians.username
INNER JOIN teachers
on event_comments.username=teachers.username
ORDER BY date DESC
LIMIT 20
This will work but assuming that a username from one table is not present in other tables, this will result into 0 rows.
a more logical approach would be to select each table then union it to join every result set like this :
SELECT e.post_id, e.event_id, e.username, e.comment, e_comments.date,
s.first_name, s.last_name, s.picture
from event_comments e
inner join students s
on e.username=g.username
UNION SELECT e.post_id, e.event_id, e.username, e.comment, e_comments.date,
g.first_name, g.last_name, g.picture
from event_comments e
inner join guardians g
on e.username=g.username
UNION SELECT e.post_id, e.event_id, e.username, e.comment, e_comments.date,
t.first_name, t.last_name, t.picture
from event_comments e
inner join teacher t
on e.username=t.username
EDIT:
To explain better about the query it just does this simple steps:
Query all comments from students using username to join post to students
Query all comments from guardians using username to join post to guardians
Query all comments from teachers using username to join post to teachers
Join results from students,guardians, teachers together
You need the using clause for each pair of joins:
FROM event_comments INNER JOIN
students
USING (username) INNER JOIN
guardians
USING (username) INNER JOIN
teachers
USING (username)
In MySQL, an inner join with no on clause is treated as a cross join. In other databases, an on or using clause is required for an inner join.
i'm working with php mysql and there are 12 tables which contains student informations.There are 3 main table First table is registration, second is demandraft and third is creditcard.The demandraft table contains all the creditcard table fields but as empty. now i want to get the whole data from these three tables to generate my xls file but coz there are empty fields of creditcard table in demandraft table so unable to fetch the whole records from all 3 tables. there is stuid field common in all 3 tables.
Here is my join query for that:
$sql = "select * from registration
join programme on registration.id=programme.stuid
join family on registration.id=family.stuid
join address on registration.id=address.stuid
join education on registration.id=education.stuid
join extradetail on registration.id=extradetail.stuid
join workexperience on registration.id=workexperience.stuid
join demanddraft on registration.id=demanddraft.stuid
join payonline on registration.id=payonline.stuid
where (DATE(registration.createddate)>='".$term1."'
AND DATE(registration.createddate)<='".$term2."')";
Use a left join.
select *
from a join b on a.id = b.a_id
will not list lines of table a that do not appear in table b.
select *
from a left join b on a.id = b.a_id
will.
Left join might be a bit tricky when chaining many of them with multiple tables. You may have to cleverly use parentheses around joins such that the order of joins is correct.
This :
$sql = "select * from registration
left join programme on registration.id=programme.stuid
left join family on registration.id=family.stuid
left join address on registration.id=address.stuid
left join education on registration.id=education.stuid
left join extradetail on registration.id=extradetail.stuid
left join workexperience on registration.id=workexperience.stuid
left join demanddraft on registration.id=demanddraft.stuid
left join payonline on registration.id=payonline.stuid
where (DATE(registration.createddate)>='".$term1."'
AND DATE(registration.createddate)<='".$term2."')";
Should do the trick