Your base structure does not even correspond to 1 normal form . Samples from relational databases with such a structure are extremely complex, because SQL is designed to work with at least some normalized data.
But even your question can be solved with pure SQL, but with some perversions. To work, we need a service table with sequence numbers, it should contain records 1,2,3,4,5 ... in a row. The number of entries in this table should certainly be more than the ID of the orders in your row. In principle, it would be possible to use any table with a suitable number of records, but I prefer sequence numbers, such a table is useful in different situations. Let's call this table seqnum and seqnum 64 entries to it:
create table seqnum(X int not null); insert into seqnum values(1); insert into seqnum values(2); insert into seqnum select X+2 from seqnum; insert into seqnum select X+4 from seqnum; insert into seqnum select X+8 from seqnum; insert into seqnum select X+16 from seqnum; insert into seqnum select X+32 from seqnum;
Next, we make a request that selects your line with the list 64 times (the number of records in seqnum ), seqnum first id from the current line and shorten the line to this first id at each iteration. Just in case, remove all spaces. Well, immediately re-glue it with your goods:
select * from ( select trim(both " " from substr(@S,1,instr(@S,",")-1)) id, @S:=substr(@S,instr(@S,",")+1) from seqnum,(select @S:=concat("2, 4, 4, 4, 4, 4",",") as S) S where @S!="" ) A, tovari B where B.id=A.id
But even after this, our query is not at all optimal, you consider php records, which is not true in the cortex, MySQL itself can count the records perfectly:
select A.*,count(1) as cnt from ( select trim(both " " from substr(@S,1,instr(@S,",")-1)) id, @S:=substr(@S,instr(@S,",")+1) from seqnum,(select @S:=concat("2, 4, 4, 4, 4, 4",",") as S) S where @S!="" ) A, tovari B where B.id=A.id group by A.id
This query will give us all the columns from the table of products and at the end another 1 column cnt with the number of such goods.
I wrote all this to show that SQL can do a lot, and not just select * from table , it is necessary to create a competent database structure, learn SQL more deeply and any work with data and development will go much faster. Your request, with the correct database structure, should look something like this ( zakTov is a table with product id and quantity of this product in the order):
select A.*,B.cnt from tovari A, zakTov B where A.id=B.tov_id and B.zakaz_id=1
Agree, it looks obviously simpler than the previous parses of the list in the line.
selectfrom 2 tables at once and get all the goods for the order with all the properties in the desired quantity. SQL is intended to select any information of interest in a single query with any conditions and connections of several tables. Ps. And I would recommend to store the goods-quantity, rather than 1,1,1 to the order -