Get last value in table depending on column values - mysql

I have a table that has 3 columns
id, Name, time
id is an incrementing int value
Name is a string identifier
time is just a epoc value of when the item was added to the table.
I can have something that looks like this.
1, Jeff, 1520288589
2, Jeff, 1520288590
3, Jeff, 1520288591
4, Tim, 1520288592
5, Jeff, 1520288593
I would like to know how I can create an SQL statement to request the last Tim row and the last Jeff row.

In your case, you can just do:
select max(id), name, max(time)
from t
group by name;
The id and time seem to both be incrementing over time.
A more general solution is:
select t.*
from t
where t.time = (select max(t2.time) from t t2 where t2.name = t.name);

Related

How to Display two table in mysql together without joining

Hello anyone can tell me what should I study about joining a table if I want to display two table with same row name? its hard to explain but I can give an example
I have 2 tables name tbwinner and tbloser.
they have the same rows ( ID, Date, Name, Points, Result )
what I want is to display all tbwinner rows with specific Date together with the tbloser asc by ID.
example
ID, Date, Name, Points, Result
1, 12-12-2001, Tester1, 50, Lose (this is from table tbloser)
2, 12-12-2001, Tester2, 90, Win (this is from table tbwinner)
3, 12-12-2001, Tester3, 87, Win (this is from table tbwinner)
1, 12-12-2001, Tester4, 40, Lose (this is from table tbloser)
I tried
SELECT *
FROM tbwinner
INNER JOIN tbloser ON tbwinner.Date = tbloser.PVDate where tblpvdeleted.PVDate ='2023-01-09' OR tblpv.PVDate = '2023-01-09'
The result show is for tbwinner only and the loser table is not displaying.
the result is half good, because only the tbwinner is display while the tbloser is not
I hope someone can understand this thanks
You can use either approach as per your need:
Using UNION
SELECT ...
FROM (
SELECT f1,f2,f3 FROM table1
UNION
SELECT f1,f2,f3 FROM table2
)
WHERE < your condition >
Multi-select (easy)
SELECT * from table1,table2

MySQL counter for specific situations

I need to generate unique "ids", the catch is, it can be only between 1 - 99999.
The "good" thing is, that it has to be unique only in group with another column.
We have groups, each group has its own "group_id" and each group need something like unique('group_id', 'increment_id')
The 99999 records is enough for several years for each group right now, but it is not enough for all groups together, therefore I cant just create table with AUTO_INCREMENT and inserting there records and taking its auto increment.
For example, if I need 5 records for Group one and three records for Group two, I suppose to get something like this:
group_id, increment_id
1, 1,
1, 2,
1, 3,
1, 4,
1, 5,
2, 1
2, 2,
2, 3
Also, the conflict is not an option, therefore using something like "length" can be tricky, if done programatically (there can be i.e. 10 requests at once, each of them first select length for given group_id and then tries to create 10 rows with same increment_id)
However I am thinking - if I set it up as the value of subselect of count, than it will always be "ok"?
You can create a auxiliar table named counters to manage that:
table: counters
columns: group_id, current_counter
OR
Each time you insert a row increment_id = select max(increment_id)+1 from table_xxx where group_id = group_xxxx
You can use user variables to get the incrementing number within each group_id:
select
t.*,
#rn := if(#group_id = group_id,
#rn + 1,
if(#group_id := group_id, 1, 1)
) increment_id
from (
select group_id
from your_table t
/* some where clauses */
order by group_id
) t
cross join (
select #rn := 0,
#group_id := - 1
) t2

SQL Query to retrieve next available ID from table

I have a table which contains a column called ticket_id and it contains values as follows:
ticket_id
STK0000000001
STK0000000002
STK0000000001
STK0000000003
STK0000000002
STK0000000001
The ticket_id value will repeat in certain rows, so it is not unique.
I am using this query to get the next available id, but I am not able to get it working. It always returns STK0000000002.
Any help is appreciated!
SQL:
SELECT
CONCAT('STK', LPAD(seq, 10, '0')) AS nextID
FROM
(SELECT
#seq:=#seq+1 AS seq,
num
FROM
(SELECT
CAST(SUBSTR(ticket_id, 4) AS UNSIGNED) AS num
FROM
sma_support_tickets
UNION ALL
SELECT
MAX(CAST(SUBSTR(ticket_id, 4) AS UNSIGNED))+2 AS num
FROM
sma_support_tickets
ORDER BY
num) AS ids
CROSS JOIN
(SELECT #seq:=0) AS init
) AS pairs
WHERE
seq!=num
LIMIT 1
Maybe I'm missing something in your question, but it seems that this should do it:
SELECT CONCAT('STK',
LPAD(MAX(SUBSTRING(ticket_id, 4)) + 1,
10,
'0')
)
FROM sma_support_tickets;
Try: This table must have one serial number or unique number or ID for the table for each row. Find out that unique number(primary key) through code and add 1 or increment that number, but not to the ticket_id as you are doing it now. so that it can move forward to next row.

Selecting * from max id when grouping by parentid (MySQL)

I have a big problem that I've been trying to tackle. Reading older similar issues hasn't been fruitful.
My table has columns [id], [parentid], [data1], [data2] etc.
There are some cases where only one record has same [parentid] and others have 2-10.
I would like to group by [parentid] and print out all data of the record which has maximum [id]. So I would only get all the "latest" details for the records which share the same [parentid] number.
I hope this is comprehensible goal in any way :).
I've also tried to tackle this in Crystal Reports XI and Qlikview 11 without major success.
Thanks in advance for any help!
Can values in your ID column be reused? If no, then juergen's answer will work.
If they can be reused, you will need to use the same table twice in your query, once to get the max id for each parent id, and once to get the row for each max id/parent id.
Like so:
select
t1.*
from aTable t1,
(select parentid, max(id) as id
from aTable group by parentid) t2
where t1.id = t2.id
and t1.parentid = t2.parentid
SQLFIddle!
select * from your_table
where id in
(
select max(id)
from your_table
group by parentid
)
A solution with qlikview would be:
Script:
Table:
Load id,
parentid,
d1,
d2
inline
[
id, parentid, d1, d2
1, 0, Hep, 01-04-2010
2, 1, Hap, 09-04-2010
3, 1, Hup, 10-10-2012
4, 2, Bep, 01-12-2009
5, 4, Ceg, 02-10-2010
6, 4, Pen, 05-10-2009
7, 4, Heg, 01-10-2009
8, 4, Ran, 08-01-2010
];
Then I added the fields id and parentid to the dashboard.
To calulate the results use a table diagram where parentid is the dimension. Add a formula
=max(id)
and name it 'max(id)'
Then you get the following result:

MySQL - Check if array values with the same id exist

I've got table with two fields: id, numbers
I want to check if the same array values (etc 1,2,5,6) already exist in table, but they have to have the same 1nd row number (id).
I've tried with this sql query, but this one doesn't check if there is the same id:
SELECT id, numbers FROM `table` WHERE numbers IN (1,2,5,6) AND id = id
I know that "id = id" doesn't work, but I posted it so you'll know what I mean.
SELECT id, COUNT(*) num_count, GROUP_CONCAT(numbers ORDER BY numbers) all_numbers
FROM `table`
WHERE numbers IN (1, 2, 5, 6)
GROUP BY id
If you only want to see the ones that have all 4 numbers, add:
HAVING num_count = 4
If you want the IDs that have all and only those 4 numbers, use:
SELECT id, COUNT(*) all_count, SUM(numbers IN (1, 2, 5, 6)) in_count
FROM `table`
GROUP BY id
HAVING all_count = in_count