I have two tables, one which stores the registered students, lets call it students_register.
The second table keeps examination details of the students, lets call it exam_details.
In the students_register table, i store:
student registration number
first name
last name
email_address
date_of_birth
and other details.
In the exam_details table, i store the registration number and the students marks in the different subjects.
Now, the challenge is that i want to query the exam_details table and display the data in a table but instead of displaying the students registration number, i want to associate the registration number in the exam_details table to that in the students_register table so that i can display the student's names instead of the registration number.
How can i go about this?
1. students_register table
id reg_number first_name last_name email_address dob
1 P2894 John Smith john#example.com 12/05/1990
2. exam-details table
id reg_number english maths chemistry biology physics
1 P2894 60% 80% 50% 72% 64%
How do i display this data in a table such that i have
first_name last_name english maths chemistry biology physics
John Smith 60% 80% 50% 72% 64%
SELECT tb2.first_name, tb2.last_name, tb1.english, tb1.maths, tb1.chemistry, tb1.bilogy, tb1.physics
FROM exam_details AS tb1
INNER JOIN students_register AS tb2
ON tb1.reg_number = tb2.reg_number
Take a look at SQL Joins -> http://www.w3schools.com/sql/sql_join.asp
With a simple JOIN query:
SELECT id, student_register.reg_number, first_name,
last_name, english, maths, chemistry, etc
FROM student_register
JOIN exam-details ON student_register.reg_number = exam-details.reg_number
There is no need to use JOIN here. I think this is the easiest way.
SELECT
sr.first_name,
sr.last_name,
ed.english,
ed.maths,
ed.chemistry,
ed.biology,
ed.physics
FROM
students_register sr,
exam_details ed
WHERE
sr.reg_number = ed.reg_numver
See for JOIN.
SELECT first_name, last_name, english, maths, chemistry, biology, physics FROM exam_details AS ex JOIN students_register st ON (ex.reg_number = st.reg_number)
SELECT * FROM student-register INNER JOIN exam-details ON student-detail.reg_number=exam-details.reg_number
may be your query. Then you print out whatever you like.
select first_name, last_name, english, maths, chemistry, biology, physics
from students_register, exam_details
where students_register.reg_number=exam_details.reg_number
You have to join these 2 tables
SELECT s.first_name, s.last_name, e.english, e.maths, e.chemistry; e.biology, e.physics FROM students_register s LEFT JOIN exam_details e USING (reg_number)
May be this will help u.
select students_register.first_name,students_register.last_name,exam-details.english, exam-details.maths, exam-details.chemistry, exam-details.biology, exam-details.physics from exam-details left join students_register on students_register.reg_number=exam-details.reg_number
Related
I have 2 table.
t_family
ID Employee
A1 John
A2 Gladys
t_sibling
ID Name Status
A1 Darco Brother
A1 Carmen Sister
A1 Clara Sister
A2 Luther Brother
I'd like to make a list by selecting Employee and Name column.
SELECT (this code i'm looking for) AS Family, Status from t_family INNER JOIN t_sibling ON t_family.ID = t_sibling.ID
The output
Family Status
John Employee
Darco Brother
Carmen Sister
Clara Sister
Gladys Employee
Luther Brother
Is it possible? Thanks in advance.
Note:
can I do it without union? just a join. I have speed issue with union. Especially the data will be hundred thousands
use union to get your desired output from both table like below
select Employee as Name,status
from
(
select id,Employee ,'Employee 'as status from t1
union all
select id,Name,status from t2
)
order by id asc
I'm really having difficulty doing a join? How can i join two table that the only relation is the username. For example:
I have two tables. tableone and tabletwo each with there own respective rows and columns.
Table One
id trans_ref username amount
2 12345 peter 50
3 45678 john 30
4 8790 frank 10
Table Two
id trans_ref username recurring status company date_order amt
1 78987 peter weekly paid new lad 12/10/2015 30
2 88776 john monthly unpaid green 15/05/2015 10
3 55667 frank yearly paid blue 17/05/2015 25
how do i perform a join so that all the values will be avail to me
$stm = $pdo->....
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row['status']; //etc
}
Since both tables have a trans_ref column, you'll need to give at least one of them an alias so you can access it distinctly from the other.
SELECT t1.trans_ref AS t1_trans_ref, t1.amount, t2.*
FROM table1 AS t1
JOIN table2 AS t2 ON t1.username = t2.username
$row['trans_ref'] will be the trans_ref column from table2, $row['t1_trans_ref'] will be the trans_ref column from table1.
Table "subjects" contains columns:
sub_date, sub_content, student_id
2014-10-03, english, 1
2014-10-09, maths, 2
2014-10-11, biology, 1
and
Table games contains columns:
game_date, game_content, student_id
2014-10-05, Hockey, 1
2014-10-18, Tennis, 1
2014-10-20, Cricket, 2
I want to display all details of student_id 1 order by date (considering both sub_date and game_date
Date, Topic
2014-10-03, english
2014-10-05, Hockey
2014-10-11, biology
2014-10-18, Tennis
Please help
you could try this:
select sub_date,sub_content from subjects where student_id = 1
UNION
select game_date,topic from games where student_id = 1
You can try with this (even though sounds not such as a good idea):
SELECT tmp.sub_date AS Date,tmp.sub_content AS Topic
FROM (
SELECT sub_date,sub_content,student_id FROM subjects
UNION SELECT game_date,game_content,student_id FROM games
) tmp
WHERE tmp.student_id = 1 ORDER BY tmp.sub_date;
We are making a simple database for our school programme. we have a table like this:
ID A B
1 John Stacey
2 Stacey Mark
3 Candice Rick
4 Stacey Rick
5 Rick Stacey
If we input Stacey's name, we display the number of rows where:
for every row (say, x and y)
rowX-ColumnA = rowY-ColumnB AND rowY-ColumnA = rowX-ColumnB
in this case, it should output:
2 rows:
4 Stacey Rick
5 Rick Stacey
Thanks! We have more than 100 students so we won't be able to do this manually. Again, I appreciate any hint.
Try this query my friend:
select t1.* from table1 t1
inner join table1 t2 on t1.A = t2.B and t1.B = t2.A;
SQL Fiddle
This will do it
SELECT p1.*
FROM People p1
INNER JOIN
People p2 ON p1.A=p2.B AND p1.B=p2.A
WHERE p1.A='Stacey' OR p1.B='Stacey'
See this SQLfiddle (SQL Server 2008)
Based on your example. I think you're probably looking for something like this:
select stu1.* from students stu1,
students stu2
where stu1.last_name = stu2.first_name
union
select stu1.* from students stu1,
students stu2
where stu1.first_name = stu2.last_name;
http://sqlfiddle.com/#!4/e9bbf/2/0
Above answer was based on your example of John Stacy , Stacy Rick.
I have two tables 'all' and 'jdetails'. I have an existing select query on the all table which works. I want to add some additional data from jdetails table if available.
all table:
judge, year, ...
jane doe, 2012
john doe, 2011
jdetails table:
name, designation,...
jane doe, level 1
jane doe, level 5
john doe, special
How do I change my query below to include the 'designation's (from jdetails) for each judge (in all)?
I think a left join is the solution but I have the where clause to consider. Also, I absolutely must have the results of this query below, but with added data from jdetails table if it exists.
Additionally, there can be multiple rows (jdetails.name) of designations for each all.judge which I want listed as a single value. e.g.-- jane doe would have designation value of 'level 1 level 5'.
I would join on all.judge=jdetails.name
current query:
$rows = $my->get_row("SELECT all.judge, `year`, `totlevel_avg`, `totlevel_count`, `genrank`, `poprank`, `tlevel_avg`, `tlevel_count`, `1level_avg` as `onelevel_avg`, `1level_count` as `onelevel_count`, `2level_avg` as `twolevel_avg`, `2level_count` as `twolevel_count`, `3level_avg` as `threelevel_avg`, `3level_count` as `threelevel_count`, `4level_avg` as `fourlevel_avg`, `4level_count` as `fourlevel_count`, `PSGlevel_avg`, `PSGlevel_count`, `I1level_avg`, `I1level_count`, `I2level_avg`, `I2level_count`, `GPlevel_avg`, `GPlevel_count`, `states` from `all` where `id` ='{$term}'");
any help is greatly appreciated.
I did not include everything you had in your SELECT statement, I just summarized it with ams.*. But the following links the all table to the jdetails table and then groups the designation into one field. Then I wrap the results in an outer query that pulls the rest of the fields you need in the all table (SQL Fiddle):
SELECT ams.*, am.Desigs
FROM
(
SELECT a.judge, GROUP_CONCAT(j.designation SEPARATOR ', ') AS Desigs
FROM `all` AS a
INNER JOIN jdetails AS j ON a.judge = j.name
GROUP BY a.judge
) AS am
INNER JOIN `all` AS ams ON am.judge = ams.judge
Here is an example that can help you:
`SELECT table1.column_name(s),table2.column_name(s) FROM table1
LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name
AND table1.column_name='Parameter'`
Where table 1 is all and table 2 is jdetails