how can i grab the last value of a coloumn - php

I have three tables
1.course(c_id(pk),c_name,sem_no);
2.student(s_id(pk),s_name,user_name,password);
3.student_info(s_id(fk),c_id(fk));
I have logged in the student, THEN i run this query
SELECT distinct sem_no FROM course,student_info WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no ;
It shows all the semester he passed including the running semester..
now
I want to show the last value of sem_no column as his current semester..
how can I grab the last value of the sem_no?
Any help will be appreciated!

Here is an improved version of what you want:
SELECT sem_no
FROM course c join
student_info si
on c.c_id = si.c_id and s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
Note the use of proper ANSI join syntax. Also notice the use of table aliases, which make the query easier to read.

Try this:
SELECT sem_no
FROM course as t1 join
student_info as t2
on t1.c_id = t2.c_id
WHERE t2.s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;

When selecting from mysql, you can order by either ascending or descending. ascending would be from 1-10 eg. and descending would be 10-1. So
SELECT distinct sem_no FROM course.student_info
WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no desc LIMIT 1;
would return last row, and only last row.

Related

Get Name From Another Table MySQL

I have two table in my database called
number_list and number_status
I am currently getting perfect result using below query
SELECT * FROM number_status
WHERE number = '".$_SESSION['number1']."' OR
number = '".$_SESSION['number2']."'
ORDER BY id DESC LIMIT $start, $limit
Now I want name from table number_list. Both table have number is common. I have tried some Left Join etc but I am learning MySQL yet so not getting proper result. Let me know if someone can help me for do it.
Thanks
You can use a join query as follows
SELECT s.id, s.number, l.name FROM number_list l JOIN number_status s ON l.number=s.number WHERE s.number = '".$_SESSION['number1']."' OR s.number = '".$_SESSION['number2']."' ORDER BY l.id DESC LIMIT $start, $limit
or you can directly get the name as you have the number in hand as follows
SELECT name FROM number_list WHERE number = '".$_SESSION['number1']."' OR number = '".$_SESSION['number2']."' ORDER BY id DESC LIMIT $start, $limit

Mysql Query search by latest date with group by

This is my table structure
and this is my dataset
What I want is query that gets data ordered by date desc and group by id_patient
so the result in the dataset example should be like this:
I would go with limit clause with subquery since you have PK :
select *
from table t
where id = (select t1.id
from table t1
where t1.id_patient = t.id_patient
order by t1.date desc
limit 1
);
However, if single patient has multiple same dates then this would produce only single records based on date.
SELECT * from rdv a JOIN (SELECT id_patient,MAX(date) date FROM rdv GROUP by id_patient ) b on a.id_patient = b.id_patient and a.date = b.date
If you want the latest record for each patient, then you are not looking for an aggregation. I would often approach this with a correlated subquery:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.id_patient = t.id_patient);
SELECT *
FROM table
GROUP BY group by id_patient
ordered by DATE(date) desc;

how to order resultset based on fields from two tables

I have two tables one for topic_likes & one for user_comments.I must get recent updates of like & comment from this tables.Given below is the sql :-
SELECT (required fields...)
LEFT JOIN topic_likes AS TL ON (TL.delete_status=0 AND TL.user_id!=$user_id)
LEFT JOIN user_comments AS UC ON (UC.delete_status=0 AND UC.user_id!=$user_id)
WHERE
(TL.created_date >= '$lastLogin' OR UC.created_date >= '$lastLogin'
ORDER BY UC.created_date desc,TL.created_date desc
LIMIT $limit
I have given order by two fields from two tables(UC.created_date, TL.created_date)
But it does not order the resultset based on created_date from topic_likes.It only orders the results based on user_comments table
But if I removed the limit condition it gives correct results...!!
Any suggestion appreciated
This is a strange approach you're taking. If you want to display user's likes and comments using a single query you should UNION the results. Example:
SELECT * FROM
(
SELECT id, `date`, 'like' as `type` FROM topic_likes
UNION
SELECT id, `date`, 'comment' as `type` FROM user_comments
) a order by a.date DESC limit 5;
The result should be similar to this:
But there are limitations. The number of columns from each subquery must match.

SQL Query Syntax error?

I am trying to modify a query which results in 2 records before the modification for some reason my modification makes it not work as it return nothing.
This Query works and returns 2 record:
$query = mysql_query("SELECT * FROM `table1`
WHERE `date` = '{$eventdate->format('Y-m-d')}'
OR `date` >= CURDATE() ORDER BY id DESC");
...the I added this: JOIN table2 USING(id)
...so this final code is this:
$query = mysql_query("SELECT * FROM `table1` JOIN `table2` USING(id)
WHERE `date` = '{$eventdate->format('Y-m-d')}'
OR `date` >= CURDATE() ORDER BY id DESC");
Problem is the second one returns nothing.
Is this a syntax error? How can I get this to work? Both tables have id fields.
Make sure that table2 contains matched data, where the id is equal to the id in table1.
You can use a LEFT JOIN if this match is not required.
id in your ORDER BY is now ambiguous. There might be more errors though. Check with mysql_error()
Try This
$query = mysql_query("SELECT * FROM `table1` a,`table2` b WHERE a.id=b.id
and (`a.date` = '{$eventdate->format('Y-m-d')}' OR `a.date` >= CURDATE())
ORDER BY id DESC")
When joining two tables which have no prefixes on the column names like - table1_id, table2_id, you should use aliases like -
SELECT * FROM table1 as t1 JOIN table2 as t2 on ...
and then you can refer to the fields in the table like this - t1.id, t2.id (you can do this also without aliases( as t1) and then you should refer to the fields like - table1.id).
The problem with your script is that the 2 tables have column id and in :
ORDER BY id DESC
the engine doesn`t know from which table do you refer this id
Other suggestion of mine is when possible not to use aggregation functions in the queries(in your query the CURDATE is that type of function). Aggregation functions in SQL prevent query caching. In our case you can pass the currdate from php to the query and the query can be cached.
Hope i`ve helped.

How can I update the second-to-last row?

I need to update the second-to-last row in a table given a set of conditions. If it was a SELECT query it would look like this:
SELECT col_1 FROM table_1 WHERE id = id_# ORDER BY timestamp_col DESC LIMIT 1,1
However, I get an error when I do the following but it's the behavior I want:
UPDATE table_1 SET col_1 = value_# ORDER BY timestamp_col DESC LIMIT 1,1
I'm aware I can write this
UPDATE table_1 SET col_1 = value_# ORDER BY timestamp_col DESC LIMIT 1
But I can't figure out how to get the second-to-last row to update given the set of conditions.
What is the correct query to update the second-to-last row?
MySQL does not support the "offset" feature in the LIMIT clause for UPDATE or DELETE operations.
But it is supported for SELECT statements.
So what you can do to get around this is wrap the offset LIMIT selection in a joined subselect, then update only the row where the id and timestamp_col equals the joined id and timestamp_col like so:
UPDATE table_1 a
INNER JOIN
(
SELECT id, timestamp_col
FROM table_1
WHERE id = <id>
ORDER BY timestamp_col DESC
LIMIT 1,1
) b ON a.id = b.id AND a.timestamp_col = b.timestamp_col
SET a.col_1 = <value>

Categories