473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

best way to show a mysql join

I have the following tables:

students
id, name,....

lessons
id,description,....

studentslessons
id,studentid,lessonid,grade

I want to list the lessons of student with name="John" for example.

The one way to do it is :
select id from students where name="John" and grab the id. then select *
from studentslessons where studentid=id;

the other is
select * from students,studentslessons where students.name="john" AND
students.id=studentslessons.studentid
Which is more efficient when using php4 and mysql 4.1? I'm using a
rather old computer Pentium 800MHz.

thanks
Feb 1 '06 #1
2 1428
G0ng wrote:
I have the following tables:

students
id, name,....

lessons
id,description,....

studentslessons
id,studentid,lessonid,grade

I want to list the lessons of student with name="John" for example.

The one way to do it is :
select id from students where name="John" and grab the id. then select *
from studentslessons where studentid=id;

the other is
select * from students,studentslessons where students.name="john" AND
students.id=studentslessons.studentid
Which is more efficient when using php4 and mysql 4.1? I'm using a
rather old computer Pentium 800MHz.

thanks


Hi

I fail to see how this is related to PHP. :P

But you can bet the second one is faster because it is only 1 query.

Many databases will first make an executionplan, then execute it.

The quality of the executionplan depends on the database, and possibly
'hints' it received.

In general: Less queries are faster then many queries, and most databases
make good executionplans.

Hope that helps.

Regards,
Erwin Moller
Feb 1 '06 #2
G0ng wrote:
I have the following tables:

students
id, name,....

lessons
id,description,....

studentslessons
id,studentid,lessonid,grade

I want to list the lessons of student with name="John" for example.

The one way to do it is :
select id from students where name="John" and grab the id. then select *
from studentslessons where studentid=id;

the other is
select * from students,studentslessons where students.name="john" AND
students.id=studentslessons.studentid
IME, I find queries that actually use JOIN to be quite a bit quicker. In
your case, you should try something similar to the following:

SELECT students.name, lessons.description, studentslessons.grade
FROM students
INNER JOIN lessons
INNER JOIN studentslessons ON studentslessons.studentid = students.id
ON lessons.id = studentslessons.lessonid
WHERE students.name = 'John'
Which is more efficient when using php4 and mysql 4.1? I'm using a
rather old computer Pentium 800MHz.


With php, the fewer queries you issue, the more efficient the code will
likely be. That way all the work is being done on the mysql server
rather than the web server. The only thing you have to do then is
optimize the queries to be faster.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Feb 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
23
by: phpfrizzle | last post by:
Hi there, I have a site with products on it. The site has a mysql backend. All products belong to certain series (table series). There can be up to 4 different products (table products)...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Phil Powell | last post by:
I have a rather complicated query with a combination of LEFT JOINs and two MATCHES where the first match is non-boolean to get the accurate score, the second to search as boolean: SELECT...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
9
by: Luke Vogel | last post by:
Hi all. This is a bit of a newbie type question. I am trying to figure out what is the best way to connect to a database; ado.net, odic others? I've found a couple of examples that show you...
0
by: yeahuh | last post by:
Quick and dirty version. Godaddy server using MySQL 4.0.24 I’m trying a left join to obtain id’s in table A(cars) that are NOT in car_id in table B(newspaper): *This is a cut down version...
0
by: jllanten | last post by:
I will appreciate any help you can provide me. In the company where i work we have a project which creates about 4-5M records daily of stats. We're currently storing this data in a db named...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.