I have the following queries.
SELECT DISTINCT goid, backid FROM trip WHERE userID = ?
SELECT goid, count(goid) FROM trip WHERE userID = ? GROUP by goid
SELECT backid , count(backid ) FROM trip WHERE userID = ? GROUP by backid
I am wondering if there is any way to maybe merge the 3 queries, or at least the last 2 queries into 1. Also if only the last 2 can be merged how can I essentially add the the results of the 2 to 1 colum eg 1011 => 20 as column backid and goid may have the same id and in this case the count should be added
Picture shows example output of 2nd query The 3rd query is exactly the same but from a different column
Minimal example:
create table trip (
goid varchar(255),
backid varchar(255)
);
insert into trip (goid, backid) values ('EGLL', 'EGLL');
insert into trip (goid, backid) values ('VABB', 'VABB');
insert into trip (goid, backid) values ('KSEE', 'BHAS');
insert into trip (goid, backid) values ('EGNM', 'YSSY');
insert into trip (goid, backid) values ('OBMD', 'KSBD');
insert into trip (goid, backid) values ('EGLL', 'VABB');
SELECT goid, count(goid) FROM trip GROUP by goid;
SELECT backid , count(backid ) FROM trip GROUP by backid;
SELECT DISTINCT goid, backid FROM trip
In the example I removed the userid as its not relevant. But main aim is for the 1st and 2nd select queries in the example to be merged into one and the out put 2 be merged, ie if EGLL in goid and backid then they are essentialy 1 key and the count added together. Over all the 2 count queries should look something similar to the picture combined