Hi! I'm fairly new to the more challenging MySQL queries. I'm currently wrestling with something that probably justifies a JOIN of some sort. Here's two SELECT statements that I want to combine;
1-
SELECT table1.col1 aa, table1.col2 ab, table1.col3 ac, table1.col4 ad, table1.col5 ae, table1.col6 af, table2.col1 ba, table2.col2 bb, table2.col3 bc FROM db.table1, db.table2 WHERE table1.col1 = "a certain number" AND table2.col1 = "some value" AND table1.col2 = table2.col2 AND table1.col4 = table2.col3 ORDER BY table1.col2 ASC
2-
SELECT table1.col1 aa, table1.col2 ab, table1.col3 ac, table1.col4 ad, table1.col5 ae, table1.col6 af, table2.col1 ba, table2.col2 bb, table2.col3 bc FROM db.table1, db.table2 WHERE table1.col1 = "a certain number" AND table2.col1 = "the oposite value from SELECT#1" AND table1.col2 = table2.col2 AND table1.col4 = table2.col3 ORDER BY table1.col2 ASC
When I combine the two like this:
3-
SELECT table1.col1 aa, table1.col2 ab, table1.col3 ac, table1.col4 ad, table1.col5 ae, table1.col6 af, table2.col1 ba, table2.col2 bb, table2.col3 bc FROM db.table1, db.table2 WHERE table1.col1 = "a certain number" AND (table2.col1 = "some value" OR table2.col1 = "the oposite value from SELECT#1" AND table1.col2 = table2.col2 AND table1.col4 = table2.col3 ORDER BY table1.col2 ASC
all values I want are shown, though on separate rows. I want to calculate percentages with the returned results. I'll explain.
Using query 3, I get:
aa | ab | ac | ad | af | ba | bb | bc
3830 | 2004-01-26 15:26:06 | 137 | 101 | 1169 | 137 | 101 | pin-mcr ok
3830 | 2004-01-26 15:26:06 | 140 | 101 | 264 | 140 | 101 | pin-mcr nok
3830 | 2004-01-26 15:29:42 | 137 | 101 | 1176 | 137 | 101 | pin-mcr ok
3830 | 2004-01-26 15:29:42 | 140 | 101 | 266 | 140 | 101 | pin-mcr nok
3830 | 2004-01-28 13:13:59 | 137 | 101 | 1192 | 137 | 101 | pin-mcr ok
3830 | 2004-01-28 13:13:59 | 140 | 101 | 282 | 140 | 101 | pin-mcr nok
3830 | 2004-01-28 13:46:08 | 137 | 101 | 1192 | 137 | 101 | pin-mcr ok
3830 | 2004-01-28 13:46:08 | 140 | 101 | 282 | 140 | 101 | pin-mcr nok
where column "af" is the counter, column "bc" a description. Now, to get the total for rows 1 & 2, which are related (pin-mcr ok & pin-mcr nok in column "bc" I need to add values "af" of both first 2 rows. pin-mcr ok + pin-mcr nok = 100%. When I have this, I can calculate the percentages pin-mcr ok & pin-mcr nok. So, is there a way to combine both query 1 & query 2, to get the entire query to show row 1 & 2 in one row, 3 & 4 in one row, 5 & 6 in one row, etcetera ...
I tried playing with left, outer & inner JOINs. I also scrutinized the MySQL forum. I'm working with a MySQL server version 3.23.58, so I can't have any obvious nor obscure UNIONs (hence the JOINs). Does anyone have a pointer in a possibly correct direction?
Very best regards,
- Alex.
1-
SELECT table1.col1 aa, table1.col2 ab, table1.col3 ac, table1.col4 ad, table1.col5 ae, table1.col6 af, table2.col1 ba, table2.col2 bb, table2.col3 bc FROM db.table1, db.table2 WHERE table1.col1 = "a certain number" AND table2.col1 = "some value" AND table1.col2 = table2.col2 AND table1.col4 = table2.col3 ORDER BY table1.col2 ASC
2-
SELECT table1.col1 aa, table1.col2 ab, table1.col3 ac, table1.col4 ad, table1.col5 ae, table1.col6 af, table2.col1 ba, table2.col2 bb, table2.col3 bc FROM db.table1, db.table2 WHERE table1.col1 = "a certain number" AND table2.col1 = "the oposite value from SELECT#1" AND table1.col2 = table2.col2 AND table1.col4 = table2.col3 ORDER BY table1.col2 ASC
When I combine the two like this:
3-
SELECT table1.col1 aa, table1.col2 ab, table1.col3 ac, table1.col4 ad, table1.col5 ae, table1.col6 af, table2.col1 ba, table2.col2 bb, table2.col3 bc FROM db.table1, db.table2 WHERE table1.col1 = "a certain number" AND (table2.col1 = "some value" OR table2.col1 = "the oposite value from SELECT#1" AND table1.col2 = table2.col2 AND table1.col4 = table2.col3 ORDER BY table1.col2 ASC
all values I want are shown, though on separate rows. I want to calculate percentages with the returned results. I'll explain.
Using query 3, I get:
aa | ab | ac | ad | af | ba | bb | bc
3830 | 2004-01-26 15:26:06 | 137 | 101 | 1169 | 137 | 101 | pin-mcr ok
3830 | 2004-01-26 15:26:06 | 140 | 101 | 264 | 140 | 101 | pin-mcr nok
3830 | 2004-01-26 15:29:42 | 137 | 101 | 1176 | 137 | 101 | pin-mcr ok
3830 | 2004-01-26 15:29:42 | 140 | 101 | 266 | 140 | 101 | pin-mcr nok
3830 | 2004-01-28 13:13:59 | 137 | 101 | 1192 | 137 | 101 | pin-mcr ok
3830 | 2004-01-28 13:13:59 | 140 | 101 | 282 | 140 | 101 | pin-mcr nok
3830 | 2004-01-28 13:46:08 | 137 | 101 | 1192 | 137 | 101 | pin-mcr ok
3830 | 2004-01-28 13:46:08 | 140 | 101 | 282 | 140 | 101 | pin-mcr nok
where column "af" is the counter, column "bc" a description. Now, to get the total for rows 1 & 2, which are related (pin-mcr ok & pin-mcr nok in column "bc" I need to add values "af" of both first 2 rows. pin-mcr ok + pin-mcr nok = 100%. When I have this, I can calculate the percentages pin-mcr ok & pin-mcr nok. So, is there a way to combine both query 1 & query 2, to get the entire query to show row 1 & 2 in one row, 3 & 4 in one row, 5 & 6 in one row, etcetera ...
I tried playing with left, outer & inner JOINs. I also scrutinized the MySQL forum. I'm working with a MySQL server version 3.23.58, so I can't have any obvious nor obscure UNIONs (hence the JOINs). Does anyone have a pointer in a possibly correct direction?
Very best regards,
- Alex.