how to connect 2 tables in php without common relation - php

I have 2 tables
From table 1(student): I want id, name, and roll_no
From table 2(score): I want SUM(total), SUM(score) with some condition
And I want all student same class data from student table combine with score table to get each student SUM(total) and SUM(score) of some condition in student table.
In score table I have student_id name column and in student
I have id column which can be linked to each other if possible...
How can I achieve this??
I want this all-in-1 query to get the data in ascending order by score column...

Related

Arrange data in MySQL tables

I have a users table and a categories table which already contain data,
I also have a third table which called user_category which has three columns(id,user_id,category_id).
When the user first register he must choose a category or more than one from a dropdown. I want to insert the id of the user to user_id columns and the id of each category to the category_id so if the user choose more than one category he will have more than one record in the user_category table.
Use This
SELECT user_id FROM users
ORDER BY user_id DESC
LIMIT 1
This will give you the last user_id which has inserted Now by using this user id you can insert the category_id by for each loop or for loop

How to count Child table to same with parent table in codeigniter?

I want to count the student name field.
i have three table
table 1
invoice
invoice_id student course_id timing_id day_id
table 2
course
course_id course_name
table 3
timing
timing_id time
table 4
days
days_id name
I want to count the number of students that are in one course, in same timing, in same day from invoice table
what is the query?
view , model, controller?
Try to learn Group by and Having in mysql then you can solve this problem

Generating duplicate value mysql query

Edited--
Table1 name is ownership_profile
Table2 name is socity_unit
Colum in Table1: NAME and UNIT_ID
Colum in Table2: wings and unit_no
How to join in one table
if there are N rows in table socity_unit with the same socity_id (as sid in table ownership_profile) then you will see the cartesian product of socity_unit X ownership_profile.
Thus, for every row in ownership_profile you will get each matching row in socity_unit.
Now, because you have a '*' in the select statement you will get all columns from both tables. If the first table has many-many columns then there is an illusion that the rows are duplicated, till you scroll to the right, in order to see the columns from second table ...
Is this your case?

Selecting Unmatched records From two tables

I need a Mysql query for my following problem.
I have two tables.
Exam with fields id and examname
Exam_Applied with fields id , examname and student_id
Here student_id is the id of student who applied the exam and I am storing in Exam_Applied
Now I need a query to Select the examname From Table Exam which are not applied by the particular student_id
help me plz.
You can use NOT IN to answer your question. Basically what it does is inside subquery, it gets all the examname taken by the specific student. The NOT IN checks the examName of table Exam which are not present on table Exam_Applied
SELECT id, examName
FROM Exam
WHERE examName NOT IN
(
SELECT examName
FROM Exam_Applied
WHERE Student_ID = 'idHERE'
)
Hope this makes sense

Is this sorting possible in MYSQL

I am now having a problem sorting the data in my PHP page where the data displayed is combined of two tables as the both tables are linked by a foreign key in one of the table.
two tables are as below
Table name: Students
stu_id
stu_name
.
.
.
stu_course_id
Table name: courses
course_id
course_name
Wen displaying the data it is displayed in following format:
Student id | Student name | student course
----1 --------------john-------------engineering
----2--------------dave---------------business
I am able to sort the data by name which is pretty easy but I am having difficulty sorting the data by course name. Is this possible as the course name is not in the same table as student?
select s.stu_id, s.stu_name, c.course_name
from students s
inner join courses c on s.stu_course_id = c.course_id
order by c.course_name asc
Of course. Simply refer to the column name without ambiguity, i.e.:
ORDER BY courses.course_name
Show me your query and I'll make that work.
Yes, use the ORDER BY clause.
SELECT * FROM courses ORDER BY course_name

Categories