A
A
Alexey Medvedev2015-09-02 14:58:43
PHP
Alexey Medvedev, 2015-09-02 14:58:43

How to group by field in SQL query?

Good day everyone! Interested in the following question.
There is a sql query in firebird:

select B.ACCOUNTID as A1, B.FIO as A2, B.ACCOUNTGTS as A3, A.NUMBERA as A4, C.MAXCONNECTIONS as A5
from TBGTS B
left outer join TBPHONES A on (A.ACCOUNTID = B.ACCOUNTID)
left outer join TBACCOUNTS C on (A.ACCOUNTID = C.ACCOUNTID)
left outer join TBACCOUNTSUPDATE D on (D.ACCOUNTID = A.ACCOUNTID and D.EMITTERKCID = B.EMITTERKCID)

The A4 field serves as a unique identifier here, the remaining 4 pains can be the same. For example,
+--+----+---+--------+----+
| 1 | 22 | 33 | uniq1 | 44 |
+--+----+---+--------+----+
| 1 | 22 | 33 | uniq2 | 44 |
+--+----+---+--------+----+
| 5 | 66 | 77 | uniq3 | 88 |
+--+----+---+--------+----+
| 5 | 66 | 77 | uniq4 | 88 |
+--+----+---+--------+----+
Is it possible to group in such a way as to get the following option:
+--+----+-- -+----------------+----+
| 1 | 22 | 33 | uniq1, uniq2 | 44 |
+--+----+---+----------------+----+
| 5 | 66 | 77 | uniq3, uniq4 | 88 |
+--+----+---+----------------+----+
The head no longer cooks, a bunch of docks have been read.
group by ....
with enumeration of fields did not give results.
Or to make such grouping by means of PHP. But again, I don’t understand how exactly ...
I ask for help from more experienced people.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-09-02
@medvedhack

GROUP_CONCAT()

select 
    B.ACCOUNTID as A1, 
    B.FIO as A2, 
    B.ACCOUNTGTS as A3, 
    GROUP_CONCAT(A.NUMBERA SEPARATOR ', ') as A4, 
    C.MAXCONNECTIONS as A5
from TBGTS B
left outer join TBPHONES A on (A.ACCOUNTID = B.ACCOUNTID)
left outer join TBACCOUNTS C on (A.ACCOUNTID = C.ACCOUNTID)
left outer join TBACCOUNTSUPDATE D on (D.ACCOUNTID = A.ACCOUNTID and D.EMITTERKCID = B.EMITTERKCID)
group by B.ACCOUNTID, B.FIO, B.ACCOUNTGTS, C.MAXCONNECTIONS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question