
Originally Posted by
wysota
Just a side note - I'd use a different query (it may be invalid but I mean the general idea):
SELECT
MAX(cardid)+1 AS 'id',
t.date,
o.name,
t.amount
FROM `transactions` AS t
INNER JOIN `operations` AS o ON t.operation=o.id
WHERE
operation>=1000 && operation<2000 GROUP BY cardid
ORDER BY date;
SELECT
MAX(cardid)+1 AS 'id',
t.date,
o.name,
t.amount
FROM `transactions` AS t
INNER JOIN `operations` AS o ON t.operation=o.id
WHERE
operation>=1000 && operation<2000 GROUP BY cardid
ORDER BY date;
To copy to clipboard, switch view to plain text mode
MAX is safer than COUNT.
But honestly that's a perfect usecase for triggers - you can have a trigger that will calculate the id itself and modify the query on the fly.
It's not a valid query:
mysql> DESCRIBE `transactions`;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| cardid | char(16) | NO | PRI | | |
| date | datetime | NO | PRI | | |
| operation | int(11) | NO | | | |
| amount | decimal(4,2) | NO | | 0.00 | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> DESCRIBE `transactions`;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| cardid | char(16) | NO | PRI | | |
| date | datetime | NO | PRI | | |
| operation | int(11) | NO | | | |
| amount | decimal(4,2) | NO | | 0.00 | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
To copy to clipboard, switch view to plain text mode
mysql> [B]SELECT * FROM transactions LIMIT 1;[/B]
+------------------+---------------------+-----------+--------+
| cardid | date | operation | amount |
+------------------+---------------------+-----------+--------+
| %07032008125843_ | 2008-03-11 14:50:32 | 1000 | 5.00 |
+------------------+---------------------+-----------+--------+
1 row in set (0.00 sec)
mysql> [B]SELECT * FROM transactions LIMIT 1;[/B]
+------------------+---------------------+-----------+--------+
| cardid | date | operation | amount |
+------------------+---------------------+-----------+--------+
| %07032008125843_ | 2008-03-11 14:50:32 | 1000 | 5.00 |
+------------------+---------------------+-----------+--------+
1 row in set (0.00 sec)
To copy to clipboard, switch view to plain text mode
As you can see I can't use MAX(cardid) because it's not correlative number. And the primary key it's done with cardid+date
Bookmarks