![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
And for another question, how do I use records from one table to select records from a second, such as: CREATE TABLE answer SELECT * FROM table1 WHERE table1.field1 table2.field1 AND table1.field2<> table2.field2; In other words, I need the records in the table "answer" to be those in table 1 that do not match records in table2 based on comparing column values. |
#3
| |||
| |||
|
|
On 10/28/2010 10:22 PM, Charles wrote: And for another question, how do I use records from one table to select records from a second, such as: CREATE TABLE answer SELECT * FROM table1 WHERE table1.field1 table2.field1 AND table1.field2<> *table2.field2; In other words, I need the records in the table "answer" to be those in table 1 that do not match records in table2 based on comparing column values. I'm not real clear on what you're asking for. *Can you post some sample data from both tables, and what you are looking for for output? |
#4
| |||
| |||
|
|
On Oct 28, 7:54 pm, Jerry Stuckle <jstuck... (AT) attglobal (DOT) net> wrote: On 10/28/2010 10:22 PM, Charles wrote: And for another question, how do I use records from one table to select records from a second, such as: CREATE TABLE answer SELECT * FROM table1 WHERE table1.field1 table2.field1 AND table1.field2<> table2.field2; In other words, I need the records in the table "answer" to be those in table 1 that do not match records in table2 based on comparing column values. I'm not real clear on what you're asking for. Can you post some sample data from both tables, and what you are looking for for output? Sure. TABLE 1 call_serial_number gps_cab_number gps_distance 100 219 5.00 101 219 6.00 102 219 7.00 103 219 8.00 100 213 8.00 101 213 7.00 102 213 6.00 103 213 5.00 TABLE 2 call_serial_number gps_cab_number date_rejected_by_cab time_rejected_by_cab 103 219 2010-10-29 00:08:15 102 213 2010-10-29 00:08:17 ANSWER call_serial_number gps_cab_number gps_distance 100 219 5.00 101 219 6.00 102 219 7.00 100 213 8.00 101 213 7.00 103 213 5.00 Answer consists of records in Table 1, minus those records in Table 2 based on a match for the call_serial_number and gps_cab_number. |
#5
| |||
| |||
|
|
On Oct 28, 7:54 pm, Jerry Stuckle<jstuck... (AT) attglobal (DOT) net> wrote: On 10/28/2010 10:22 PM, Charles wrote: And for another question, how do I use records from one table to select records from a second, such as: CREATE TABLE answer SELECT * FROM table1 WHERE table1.field1 table2.field1 AND table1.field2<> table2.field2; In other words, I need the records in the table "answer" to be those in table 1 that do not match records in table2 based on comparing column values. I'm not real clear on what you're asking for. Can you post some sample data from both tables, and what you are looking for for output? Sure. TABLE 1 call_serial_number gps_cab_number gps_distance 100 219 5.00 101 219 6.00 102 219 7.00 103 219 8.00 100 213 8.00 101 213 7.00 102 213 6.00 103 213 5.00 TABLE 2 call_serial_number gps_cab_number date_rejected_by_cab time_rejected_by_cab 103 219 2010-10-29 00:08:15 102 213 2010-10-29 00:08:17 ANSWER call_serial_number gps_cab_number gps_distance 100 219 5.00 101 219 6.00 102 219 7.00 100 213 8.00 101 213 7.00 103 213 5.00 Answer consists of records in Table 1, minus those records in Table 2 based on a match for the call_serial_number and gps_cab_number. |
#6
| |||
| |||
|
|
On 2010-10-29 09:25, Charles wrote: On Oct 28, 7:54 pm, Jerry Stuckle <jstuck... (AT) attglobal (DOT) net> wrote: On 10/28/2010 10:22 PM, Charles wrote: And for another question, how do I use records from one table to select records from a second, such as: CREATE TABLE answer SELECT * FROM table1 WHERE table1.field1 table2.field1 AND table1.field2<> *table2.field2; In other words, I need the records in the table "answer" to be those in table 1 that do not match records in table2 based on comparing column values. I'm not real clear on what you're asking for. *Can you post some sample data from both tables, and what you are looking for for output? Sure. TABLE 1 call_serial_number * * * * gps_cab_number *gps_distance 100 * * * *219 * * 5.00 101 * * * *219 * * 6.00 102 * * * *219 * * 7.00 103 * * * *219 * * 8.00 100 * * * *213 * * 8.00 101 * * * *213 * * 7.00 102 * * * *213 * * 6.00 103 * * * *213 * * 5.00 TABLE 2 call_serial_number * * * * gps_cab_number *date_rejected_by_cab time_rejected_by_cab 103 * * * *219 * * 2010-10-29 * * *00:08:15 102 * * * *213 * * 2010-10-29 * * *00:08:17 ANSWER call_serial_number * * * * gps_cab_number *gps_distance 100 * * * *219 * * 5.00 101 * * * *219 * * 6.00 102 * * * *219 * * 7.00 100 * * * *213 * * 8.00 101 * * * *213 * * 7.00 103 * * * *213 * * 5.00 Answer consists of records in Table 1, minus those records in Table 2 based on a match for the call_serial_number and gps_cab_number. MySQL does not support the except (sometimes named minus) operator, but you can express the same thing via: select call_serial_number, gps_cab_number, gps_distance from table1 t1 where not exists ( * * * * select 1 from table2 t2 * * * * where (t1.call_serial_number, t1.gps_cab_number) * * * * * * = (t2.call_serial_number, t2.gps_cab_number) ); or by using an outer join: select call_serial_number, gps_cab_number, gps_distance from table1 t1 left join table t2 * * on (t1.call_serial_number, t1.gps_cab_number) * * *= (t2.call_serial_number, t2.gps_cab_number) where t2.call_serial_number is null; If either call_serial_number or gps_cab_number is nullable you may have to add additional conditions to get the semantics right. |
#7
| |||
| |||
|
|
If either call_serial_number or gps_cab_number is nullable you may have to add additional conditions to get the semantics right. |
#8
| |||
| |||
|
|
If either call_serial_number or gps_cab_number is nullable you may have to add additional conditions to get the semantics right. And while there is no null field in either table, one cold pray and hope (and plan) for gps_match_cab_reject - table 2 in the example - to be empty , I suppose. |
#9
| |||
| |||
|
|
And are you using "outer join" or "left join" in the statement? You preface it by explaining it as a "outer join" (digging for dictonary, but that's not relevant), but the in the statement you state "left join". |
#10
| |||
| |||
|
|
On 2010-10-29 13:36, Charles wrote: [...] And are you using "outer join" or "left join" in the statement? *You preface it by explaining it as a "outer join" (digging for dictonary, but that's not relevant), but the in the statement you state "left join". left join is a shorter form of left outer join, Both variants are legal according to standard. select T1.x, T2.y from T1 left outer join T2 * *on ... is eqvivalent with (any complications introduced with nulls ignored): select T1.x, T2.y from T1 inner join select ... from T2 * *on ... union select T1.x, cast(null as <type of T2.y>) as y... from T1 where not exists ( * * select 1 from T2 * * where ... ) Hence: select T1.x, T2.y from T1 left outer join T2 * *on ... where T2.y is null is eqvivalent with select T1.x, cast(null as <type of T2.y>) as y... from T1 where not exists ( * * select 1 from T2 * * where ... ) Since you asked about the second solution I assume that the first one is clear to you. |
![]() |
| Thread Tools | |
| Display Modes | |
| |