I want to select latest row from approver table based on a request id.
Also i need to join request table and approver table based on request id
Tried latest() method but it will return only last record. But i want to retrieve last last row of each request id.
Tried group by and distinct but not worked. Could you please suggest a better way
maybe this would be work, use orderBy('column_name',desc)->first()
there are two way to do this :
code a loop and get rows by a query for each row.
try this code :
\DB::table('my_table')->selectRaw('*,max(id) as last')->groupBy('user_id')->get();
Use this query:
$last_approver = Approver::orderBy('id', 'DESC')->first();
select * from approver where request_id = <your request id> order by <some column like created_at> limit 1
Related
I got a database that registers user actions and their geolocation.
Now I would like to fetch this data at the hand of the last action per user.
The table looks a bit like:
geoaction_id AUTO INCREMENT
geoaction_user
geoaction_creationdate (Y-m-d H:i:s)
geoaction_action
geoaction_lon
geoaction_lat
Now I would like to make a simple query that selects of all users the last item.
But LIMIT 0,1 just parses one row no matter what. (LOGICALLY!!)
Group by gives a little better result.
But how to get only the last item per user?
Try this, please provide the queries you have checked out so far, in order to assist you better.
SELECT geoaction_user, geoaction_action
FROM table-name
GROUP BY geoaction_user
ORDER BY geoaction_action DESC LIMIT 1
Working with sets:
SELECT
g.geoaction_user,
g.geoaction_action,
g.geoaction_creationdate,
g.geoaction_lat,
g.geoaction_lon
FROM
(
SELECT
geoaction_user,
MAX(geoaction_id) max_id
FROM
geoactions
GROUP BY geoaction_user
) s
JOIN
geoactions g
ON s.geoaction_user = g.geoaction_user
AND s.max_id = geoaction_id
The subquery generates a virtual table with the geoaction_id from the latest entry in the tabble for each user_id, then the table is joined to get the data belong to the latest id.
If you need to filter out some records place the where clause in the subquery
How can i using Codeigniter or SQL to count the number of rows where they have the same value (Table: responds, Row: task_id), then use the task_id to print out the results from the table that contains the information (Table: tasks).
I hope you understand.
While I'm not 100% sure I understand your question, if you're trying to get the count of taskids in the responds table and link that to the tasks table, have you tried something like this:
select t.taskid, t.taskname, r.taskcount
from tasks t
join
(
select taskid, count(taskid) taskcount
from responds
group by taskid
) r
on t.taskid = r.taskid
And the SQL Fiddle.
How to receive the last row id in the database without generating a new row or updating the row.
Just to read the last id?
I have the function mysql_insert_id and need something like this, but without generating a new row.
SELECT MAX(id) AS latest_id
FROM table_name
If you instead want the comming (and not yet existing) id without inserting a row, see my previous answer.
You need to use max() function like
select max(yourcol) from yourtable
You can try lot of queries for that :-
select * from TABLE_NAME order by ID desc limit 1;
"or"
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);
I'm currently using:
SELECT MAX(id) FROM table
To discover the current id of a certain table, but i heard this can bring bad results. What is the proper way of doing that? Please, notice that i'm not INSERTING or DELETING anything before that query. I just want to know the current ID, without prior INSERT or DELETE.
Perform the following SQL:
SHOW TABLE STATUS LIKE 'TABLENAME'
Then check field AUTO_INCREMENT
You can use the following query:
SELECT id FROM table ORDER BY id DESC LIMIT 1;
I need to get next id from table (auto_increment).
I could just use SELECT * from table ORDER BY id DESC LIMIT 1;
For example I get 50. But if we delete from table two items I will get 48 but correct one
will be 51. How get correct value even we something delete from table ?
You can only use SHOW TABLE STATUS LIKE 'tablename' to fetch the auto_increment value. A simpler solution might be: SELECT MAX(id) + 1 FROM table, but this is buggy if the last entry was deleted.
show table status like 'table_name'
next id value is in 'Auto_increment' field
SHOW TABLE STATUS LIKE 'table'
The value you want is in the Auto_increment field.
Be careful about concurrency though: by the time you get around to using this value, some other client could have inserted into the table and thus your value is out of date. It's usually best to try to not need this.
SELECT LAST_INSERT_ID() + 1;
gets the last ID used in an insert in an autoincrement column + 1
I see two solutions for the next ID:
1) Select bigger value of a column with max function. Example: select max( id ) from table;
2) Using the command SHOW STATUS LIKE and get the correct index of array. Take a look: http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
Seems to me you're creating a race condition here.
Why exactly can you not insert the row you want to insert and then use LAST_INSERT_ID() to find it's ID?