I have a table called Grades and inside I have some columns:
Id id_student Subjects grade
3 1 [->] Biology 8
4 1 [->] Math 4
5 1 [->] Sports 4
6 1 [->] Math 8
7 1 [->] English 9
8 4 [->] Sports 10
9 4 [->] English 7
I selected everything from this table where id_student = student logged in
$sth = $this->dbh->prepare("SELECT * FROM note WHERE id_elev = :id_elev");
$sth->bindParam(":id_elev", $_SESSION['id']);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
So all I selected is this:
Id id_student Subjects grade
3 1 [->] Biology 8
4 1 [->] Math 4
5 1 [->] Sports 4
6 1 [->] Math 8
7 1 [->] English 9
I want to make a average grade for Math , Biology , etc for this student but I want it without making a SELECT * FROM Grades WHERE $result['Subjects'] == 'Biology' because some students may not have this subject in their schedule.
I want something like this:
Id id_student Subjects grade
4 1 [->] Math 4
6 1 [->] Math 8
But without WHERE $results['Subjects'] == 'Math';
I'm sorry it is hard to explain when you don't know a lot of english. I hope that someone understands what I wrote and give me an advice. I can make another table called Subject if it is necessary.
Check out this fiddle:
http://sqlfiddle.com/#!2/8ee5a/3
I think it does what you need. If not, it's a great place to test the query.
I used the GROUP BY function AVG() to get the average grade for each subject.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_avg
Here is the schema and query:
CREATE TABLE grades
(
Id int auto_increment primary key,
id_student int,
Subjects varchar(20),
grade int(1)
);
INSERT INTO grades
(Id, id_student, Subjects, grade)
VALUES
('3', '1', 'Biology', 8),
('4', '1', 'Math', 4),
('5', '1', 'Sports', 4),
('6', '1', 'Math', 8),
('7', '1', 'English', 9);
SELECT id_student, Subjects, AVG(grade) FROM grades GROUP BY Subjects;
The query below is an example of the use I mentioned in my comment
-- The AVG() function returns the average value of a numeric column.
-- because you are executing an aggregate function on the Grade column you have to specify which columns to group the results together by.
SELECT
Id
,Subject
,AVG(Grade) as 'Average Grade'
FROM
Result
GROUP BY
Id
,Subject
Related
Is there any way I could use php+mysql to do multiple criteria search?
I saw it on a website, and i want to make something similar for myself.
So I have two tables
table1- 'Books' (title, author, BookID)
table2- 'Descriptions' (Bookid, descriptions)
Each book can have as many descriptions as the users want it to have.
Table 'Books'
Title Author BookID
Nightmare (…) Tom B. AAA
Wonderful (…) Jim C. AAB
Ghost Night (…) Amy D. AAC
Babysitter (…) Kitty D. AAD
Hello world (…) Pete P. AAE
Hannibal (…) Butter H. AAF
and
table 'Descriptions'
Book ID Description Relevance
AAA Thriller 0
AAA Crime 0
AAA Detective 0
AAB Geography 0
AAB Travel 0
AAC Thriller 0
AAD Comedy 0
AAD Family 0
AAE Programming 0
AAE Computer Science 0
AAF Thriller 0
AAF Crime 0
I have already made the insert data part, it's actually not that hard for me, but when it comes to the searching part, things become way more complicated than I thought it would be.
the sheet I'll be using is the exact same model my ‘target’ website uses,here i use () to represent html's select form, and [] to represent ordinary input form
[ ]
(AND/OR) [ ]
(AND/OR) [ ]
(AND/OR) [ ]
and users can fill it 1-4 criteria, for example, "thriller" (AND) "Crime", and the MySQL would find all the descriptions that fit these descriptions, in my case, it would be these informations:
AAA Thriller 0
AAA Crime 0
AAC Thriller 0
AAF Thriller 0
AAF Crime 0
And next step the computer should categorize the ID found and sort the results into this:
AAA 2 criteria verified
AAF 2 criteria verified
AAC 1 criteria verified
and after that, i want it to echo/print these information onto the browser:
1. Nightmare (…) Tom B. AAA
descriptions: Thriller, Crime
2. Hannibal (…) Butter H. AAF
descriptions: Thriller, Crime
3. Ghost Night (…) Amy D. AAC
descriptions: Thriller
my main problem is the categorizing and sorting part, and i don't know how to insert a code into another code in SQL.
I’ve tried GROUP BY (for the first time) and I didn’t make it.
in fact, after a few days of failed attempts, i started thinking if my data structure is the real problem...
thank you for your help and your attention.
Using a group by is a good idea, but you might want to add a count of catergories to help you to sort.
In example :
Schema (MySQL v5.7)
CREATE TABLE book (
`title` VARCHAR(10),
`id` VARCHAR(3) PRIMARY KEY
);
INSERT INTO book
(`title`, `id`)
VALUES
('Nightmare', 'AAA'),
('Wonderful.', 'AAB'),
('GhostNight', 'AAC'),
('Babysitter', 'AAD'),
('Helloworld', 'AAE'),
('Hannibal', 'AAF');
CREATE TABLE cat (
`BookID` VARCHAR(3),
`Description` VARCHAR(50),
`Relevance` VARCHAR(7),
FOREIGN KEY (`BookID`) REFERENCES book (id)
);
INSERT INTO cat
(`BookID`, `Description`, `Relevance`)
VALUES
('AAA', 'Thriller', '0'),
('AAA', 'Crime', '0'),
('AAA', 'Detective', '0'),
('AAB', 'Geography', '0'),
('AAB', 'Travel', '0'),
('AAC', 'Thriller', '0'),
('AAD', 'Comedy', '0'),
('AAD', 'Family', '0'),
('AAE', 'Programming', '0'),
('AAE', 'Computer Science', '0'),
('AAF', 'Thriller', '0'),
('AAF', 'Crime', '0');
Query #1
SELECT b.title, COUNT(c.description) AS cnt
FROM book b
LEFT JOIN cat c
ON b.id = c.BookID
WHERE c.Description IN ('Thriller', 'Crime')
GROUP BY b.title
ORDER BY cnt DESC;
Output
| title | cnt |
| ---------- | --- |
| Nightmare | 2 |
| Hannibal | 2 |
| GhostNight | 1 |
View on DB Fiddle
Here is my table looks..
Users
Id name height weight
1 aaa 1 10
2 bbb 4 104
3 ccc 1 10
4 ddd 56 150
5 eee 232 180
second table looks like
Profile view
Id sender receiver block
1 1 2 True
2 2 3 False
3 4 1 False
The problem i am facing is,,When I search using height and weight in users table and block using profileview table.I couldn't get proper results..
If second user bbb search with height "1" and weight "10" it should be appear 3rd user details ccc .First user also matched but First user blocked second user.The problem is when i using join values coming if sender and receiver exists i the profileview table.If not exist how do we do in joins..
CREATE TABLE #Users
(
Id int,
[Name] varchar(255),
Height int,
[Weight] int
)
CREATE TABLE #ProfileView
(
Id int,
Sender int,
Receiver int,
[Block] varchar(5)
)
INSERT INTO #Users
(Id, [Name], Height, [Weight])
VALUES
(1, 'aaa', 1, 10),
(2, 'bbb', 4, 104),
(3, 'ccc', 1, 10),
(4, 'ddd', 56, 150),
(5, 'eee', 232, 180)
INSERT INTO #ProfileView
(Id, Sender, Receiver, [Block])
VALUES
(1, 1, 2, 'True'),
(2, 2, 3, 'False'),
(3, 4, 1, 'False')
DECLARE
#CallingUser int, --User performing the search
#Height int, --Height searched for
#Weight int --Weight searched for
SET #CallingUser = 2
SET #Height = 1
SET #Weight = 10
SELECT
*
FROM
#Users
LEFT OUTER JOIN #ProfileView
ON #Users.Id = #ProfileView.Id
AND #ProfileView.Receiver = #CallingUser
WHERE
#Users.Id <> #CallingUser
AND #Users.Height = #Height
AND #Users.[Weight] = #Weight
AND (#ProfileView.[Block] = 'False' OR #ProfileView.[Block] IS NULL)
DROP TABLE #Users
DROP TABLE #ProfileView
from this data how can i fetch all users where user_id in tbl_attendance not having current date with application_status 3 and 4
attendance_id user_id date_in attendance_status status 0=absent 1=present 3 = onleave 4 = onoff
1 1 2017-02-05 1
2 36 2017-02-11 4
3 36 2017-02-11 4
4 36 2017-02-11 3
5 1 2017-02-02 1
6 36 2017-02-01 1
my code is like this
$date=date('Y-m-d');
$this->db->where('tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in= "'.$date.'" )');
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
want to combine those two lines and produce the output like if current date is present in 3 or 4 please help me
You can try like this.To combine two conditions
$this->db->where("tbl_users.user_id NOT IN(SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in='$date' AND tbl_attendance.attendance_status IN(3,4))");
seeing you're using query builder...
$this->db->group_start()
$this->db->where_in( 'tbl_attendance.attendance_status', array( '3', '4' ) );
$this->db->where_not_in('tbl_users.user_id', 'SELECT user_id FROM tbl_attendance WHERE tbl_attendance.date_in = '. $date, false)
$this->db->group_end()
should work
I wish to select all records from a database and then combine matching keys and also adding their values.
Col1 col2
----- ------
ABC 2
ABA 3
ADD 1
AED 3
ABC 2
ABA 3
ADD 1
AED 3
So i would end up with
array("
ABC => 4,
ABA => 6,
ADD => 2,
AED => 6");
This is basic GROUP BY application:
SELECT Col1, SUM(col2) FROM tbl GROUP BY Col1
See http://sqlfiddle.com/#!2/8d1f8/1 for an example
This question already has answers here:
sql table how to multliply array of numbers fetched from sql table and stored into array and return
(3 answers)
Closed 9 years ago.
Table like given below
Id country Person Money sum
1 UK john 2010 null
2 USA Henry 120 null
3 RUS neko 130 null
4 GER suka 110 null
7 CAN beater 1450 null
8 USA lusi 2501 null
each money column multiply by 2 and that stored into corresponding sum column how, like given below
Id country Person Money sum
1 UK john 2010 4020
2 USA Henry 120 240
3 RUS neko 130 260
4 GER suka 110 220
7 CAN beater 1450 2900
8 USA lusi 2501 5002
You just want to update the field using an expression like this.
update `tablename` set `sum` = `Money` * 2 where `sum` is null;
If you want this to happen indescriminately you can drop the where portion of the update like:
update `tablename` set `sum` = `Money` * 2;