mysql - Select data from the last complete row -
i have spreadsheet being migrated mysql database. in future spreadsheet won't filled in. data uploaded directly database. spreadsheet in 2 tables. one, called piles, contains info on product. other, called cubes, contains test results performed on products. sample data 2 tables below:
piles:
+----------+------------+---------+-----------------+ | id | date | mouldid | stockcode | +----------+------------+---------+-----------------+ | 23a42895 | 2017-06-09 | 23a | 250cdj09.0/0412 | | 23b42895 | 2017-06-09 | 23b | 250cdj07.0/0412 | | 642895 | 2017-06-09 | 6 | 250cdj09.0/0412 | | 8642895 | 2017-06-09 | 86 | 250cdj07.0/0412 | | 842895 | 2017-06-09 | 8 | 250cdj09.0/0412 | +----------+------------+---------+-----------------+
cubes:
+---------+------------+-------------+---------+ | id | date | conctotalm3 | mouldid | +---------+------------+-------------+---------+ | 2342895 | 2017-06-09 | 18.1 | 23 | | 4842895 | 2017-06-09 | 57.9 | 48 | | 842895 | 2017-06-09 | 108.4 | 8 | | 1542895 | 2017-06-09 | 154.7 | 15 | | 6242895 | 2017-06-09 | 204.6 | 62 | +---------+------------+-------------+---------+
now spreadsheet filled in hand. when mould id , date matched info written on 1 row. data in cubes table dragged down until matching row found , process repeated.
my question is, how can write sql query output same. can ofc write query joins on date , mouldid won't "drag data down" note if numeric part of mouldid matches match row. , b doesnt matter. have included expect output sample data reference.
+----------+------------+---------+-----------------+-------------+ | id | date | mouldid | stockcode | conctotalm3 | +----------+------------+---------+-----------------+-------------+ | 23a42895 | 2017-06-09 | 23a | 250cdj09.0/0412 | 18.1 | | 23b42895 | 2017-06-09 | 23b | 250cdj07.0/0412 | 18.1 | | 642895 | 2017-06-09 | 6 | 250cdj09.0/0412 | 18.1 | | 8642895 | 2017-06-09 | 86 | 250cdj07.0/0412 | 18.1 | | 842895 | 2017-06-09 | 8 | 250cdj09.0/0412 | 108.4 | +----------+------------+---------+-----------------+-------------+
edit: have removed columns clarity think confusing things. don't have using join mouldid. @ moment using script tells me position of letter in string. use left() number part only.
what can't join work use last match if can't match current row. explain example output first 2 lines matches there mould 23 in cubes table. mould 6 doesn't have match uses last match (from row above). same occurs mould 86. mould 8 have match in cubes table uses that. hope clears things bit more. thanks
sql fiddle reference: http://sqlfiddle.com/#!9/6f19df/2
you use pair of nested replace avoid , b , match cubes.id
select date , a.mouldid , a.stockcode , a.length , a.serials , a.printnum , b.conctotalm3 , b.quantity , b.time .... piles inner join cubes b on replace(replace(a.id, 'a', '') , 'b','') = b.id
looking sqlfiddle match
a.date , a.id , b.id , replace(replace(a.id, 'a', '') , 'b','') , a.mouldid , a.stockcode , b.conctotalm3 piles inner join cubes b on replace(replace(a.id, 'a', '') , 'b','') = b.id
Comments
Post a Comment