how to find the highest value in mysql table - php

i have a mysql table i.e
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
my question is how can i find the highest subject score, the second last and so on
Note that all the subject fields have int(11) values

Break your database into 3 tables like:
Students:
st_id | name | email
1 | john |#a.com
Courses:
cr_id | name
1 | maths
2 | chemistry
3 | bio
4 | social_studies
StudentCourses:
st_id | cr_id | score
1 | 1 | 20
1 | 2 | 23
1 | 3 | 10
1 | 4 | 15
Now you can do:
SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;

SELECT * FROM <table>
ORDER BY <field> DESC
LIMIT <needed number of rows>
Example:
SELECT * FROM <table>
ORDER BY maths+chemistry+bio+social_study DESC
LIMIT 3

Strictly PHP method: I assume you want to maintain association with field names. In that case, just use asort($row); on each row in your query result, assuming you fetched the row as an array. asort will sort the array from lowest value to highest (with additional flags to tweak the results if needed), while maintaining keys. A foreach loop will then allow you to work with each key/value pair in the sorted order.

st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
The query can be for top marks
SELECT id,GREATEST(mark,mark1,mark2) AS `top` FROM `students`

Related

Search Array values in comma Separated Column in Laravel

I have two tables (Assessments, Unit_Assigned). In Assessments table I store Multiple Unit Assigned Ids with comma-separated Format like below
id | title | assignedids
-------------------------
2 | NTitl | 15,25,6
-------------------------
3 | Ctitl | 25,6,38
Unit_Assigned Table display below
id | staffid | unit_id | batchid
---------------------------------
15 | 10 | 25 | 31
---------------------------------
38 | 12 | 18 | 42
---------------------------------
I need to get an assessment list based on staff ID (available in Unit_Assigned table)
I got assigned ids list using below query
$assignedunits=Unitassigning::Where('staffid',$staffid)->get()->pluck('id')->toArray();
try to fetch assessment using below query
$stringconverted=implode(",",$assignedunits);
$completedAssessments=Assessments::whereRaw("find_in_set('$stringconverted',assignedids)")->get();

Select foreign key (group) where is the biggest match

I have three tables group_sentences, group_sentences_attributes and group_senteces_categories.
I have an attributes array which I am using in query with IN (after implode).
Then I have one category ID because they are stored recursively, so no need for an array.
I need to select one group number where is the biggest match for $attributesArray and of course category too.
Here is table group_sentences_attributes
+-----+-------+-----------+
| id | group | attribute |
+-----+-------+-----------+
| 1 | 1 | 3564 |
| 2 | 1 | 3687 |
| 3 | 1 | 3689 |
| 4 | 2 | 3687 |
| 5 | 2 | 3564 |
+-----+-------+-----------+
Here is group_sentences_category
+-----+-------+----------+
| id | group | category |
+-----+-------+----------+
| 1 | 1 | 1564 |
| 2 | 1 | 1221 |
| 3 | 1 | 1756 |
| 4 | 2 | 1358 |
| 5 | 2 | 1125 |
+-----+-------+----------+
Here is my query, but I am afraid that it won't do the job done.
SELECT group_categories.group
FROM group_categories, group_attributes
WHERE group_categories.category = '$category'
AND group_attributes.attribute IN ($attributesArray)
GROUP BY group_categories.group
ORDER BY count(group_attributes.attribute)
Any help would be appreciated, thanks.
First, the table in your query do not match the tables in the question. I am guessing they are simply missing the "sentence". Then, you have no join clause. Simple rule: Never use commas in the from clause.
group is a lousy name for a column, because it is a keyword in SQL. The following may be what you are looking for:
SELECT gc.groupid
FROM group_sentences_attributes sa JOIN
group_sentences_category sc
ON sa.groupid = sc.groupid
WHERE sc.category = '$category' AND
sa.attribute IN ($attributesArray)
GROUP BY sa.groupid
ORDER BY count(sa.attribute);
If you only want one row, then add LIMIT 1 to the end.

Can this be done in a single query?

I have 3 tables in my database, in which team1 and team2 ids in Matches Table are equal to team_id in TeamNames Table.
also group in Matches Table is equal to group_id in GroupNames Table
Matches Table
-----------------------------------
| team1 | team2 | group | count |
| 3 | 5 | 1 | 1 |
| 1 | 2 | 3 | 0 |
-----------------------------------
GroupNames Table
-----------------------
| group_id | name |
| 1 | Finals |
| 3 | Semi-Final |
-----------------------
TeamName Table
-----------------------
| team_id | name |
| 5 | Flowers |
| 2 | Rainbow |
-----------------------
What I need to get is:
SELECT team1 , team1_name , team2 , team2_name , group , group_name WHERE count=1
I tried joining tables, but as each of the team1 and team2 should be related to unique id in TeamName Table I failed, getting group name was easy, but I failed getting all the above in single query
Questions:
Is this possible in single query?
Can this be done using CodeIgniter's "Active Record Class"?
Answer for q1:yes
Answer for q2:yes
Hope this may help you
$this->db->from('Matches m');
$this->db->select('m.team1,m.team2,m.group,m.count,tn1.name team1_name,tn2.name team2_name,gn.name group_name');
$this->db->join('TeamName tn1','tn1.team_id = m.team1');
$this->db->join('TeamName tn2','tn2.team_id = m.team2');
$this->db->join('GroupNames gn','gn.group_id = m.group');
$this->db->where('m.count',1);
$results=$this->db->get()->result();
yes. with INNER JOIN.
Here is an example to combine your three tables, if you want you can combine more.
SELECT * FROM matches ma INNER JOIN groups gr ON ma.groupid = gr.groupid INNER JOIN teams te ON ma.teamid = te.teamid WHERE ma.count = 1
Good luck!
Martin

SQL QUERY multiple search in one row to find data from another row in the same table

I need some help with some code
I have a database table called "stuff" and I have this info:
+------+-------------+---------------------+
| id | member_id | group_id |
+------+-------------+---------------------+
| 1 | 11 | aa |
+------+-------------+---------------------+
| 2 | 22 | aa |
+------+-------------+---------------------+
| 3 | 33 | aa |
+------+-------------+---------------------+
| 4 | 44 | bb |
+------+-------------+---------------------+
| 5 | 55 | bb |
+------+-------------+---------------------+
| 6 | 66 | bb |
+------+-------------+---------------------+
I need to find the group id if I search all 3 members from one group
Something like:
SELECT group_id
FROM stuff
WHERE member_id=11 and member_id=22 and member_id=33
I know the query it is not valit but I don`t know how to make it valid.
Thank you very much.
The problem is called Relational Division.
SELECT group_id
FROM stuff
WHERE member_id IN (11,22,33)
GROUP BY group_id
HAVING COUNT(*) = 3
SQLFiddle Demo
if member_id is not unique for every group_id, you need to have DISTINCT in order to count only unique values.
SELECT group_id
FROM stuff
WHERE member_id IN (11,22,33)
GROUP BY group_id
HAVING COUNT(DISTINCT member_id) = 3
More variations on this link:
SQL of Relational Division

PHP MYSQL Output Uniform Value to same Table Column

I have a table in my MySQL like the below
============================
id | courses | r_number
----------------------------
1 | English | C/009
2 | Maths | C/009
3 | English | C/003
4 | Maths | C/002
============================
How do I ouput this to be like the HTML table below
====================================
id | courses | r_number
------------------------------------
1 | English, Maths | C/009
2 | English | C/003
3 | Maths | C/002
====================================
SELECT MIN(ID) ID,
GROUP_CONCAT(Courses SEPARATOR ', ') Courses,
r_Number
FROM TableName
GROUP BY r_Number
ORDER BY ID
SQLFiddle Demo
I'm wondering why in your example the ID doesn't match with the record, why is that?

Categories