| 156 | |
| 157 | == Example 5 == |
| 158 | We wanted to query all custom_ticket.value of a certain type (custom_ticket.name), where a different custom_ticket.value of the same ticket was met. |
| 159 | |
| 160 | '''trac.ini:''' |
| 161 | {{{ |
| 162 | [ticket-custom] |
| 163 | duration = text |
| 164 | duratino.label = Duration |
| 165 | |
| 166 | customer = text |
| 167 | customer.label = Customer |
| 168 | }}} |
| 169 | |
| 170 | This obviously didn't work: |
| 171 | |
| 172 | '''Custom SQL:''' |
| 173 | |
| 174 | {{{ |
| 175 | SELECT |
| 176 | ticket_custom.value as Duration, |
| 177 | FROM ticket_custom |
| 178 | WHERE ticket_custom.name = 'duration' |
| 179 | AND ticket_custom.value = 'Our Customer' |
| 180 | }}} |
| 181 | |
| 182 | What we did was make a "loop" INNER JOIN ticket_custom on ticket_custom.ticket like this: |
| 183 | |
| 184 | '''Custom SQL:''' |
| 185 | |
| 186 | {{{ |
| 187 | SELECT |
| 188 | tc1.value as Duration, |
| 189 | FROM ticket_custom tc1 |
| 190 | INNER JOIN ticket_custom tc2 |
| 191 | ON tc1.ticket = tc2.ticket |
| 192 | WHERE tc1.name = 'duration' |
| 193 | AND tc2.value = 'Our Customer' |
| 194 | }}} |