Welcome Guest, Not a member yet? Register   Sign In
Selecting records based on ID
#14

[eluser]kgill[/eluser]
[quote author="RS71" date="1236817717"]Thanks guys

The UNION is pretty helpful. I have some questions though:

Is there a way to differentiate the rows based on table? So that in PHP I can separate the results?
[/quote]

Take a look at the example I gave again - that first column is a constant and differentiates what came from what table.

[quote author="RS71" date="1236817717"]
In the result, it combines all fields even books and shows into one field called 'books'. What happens in UNIONs with non similar fields?[/quote]

UNION only works when you have the same number of fields and the fields are the same types, what you're doing is combining the results of two queries into one result set. The only reason it called the field books is that was the first column name it found, if you had the shows query first it would have called them shows - use an alias for the column if you find it confusing. As for performance gains, you're making one less trip to the database but depending on what you're doing with the data in the view it may or may not make a difference.


Messages In This Thread
Selecting records based on ID - by El Forum - 03-09-2009, 03:51 PM
Selecting records based on ID - by El Forum - 03-09-2009, 03:58 PM
Selecting records based on ID - by El Forum - 03-09-2009, 04:04 PM
Selecting records based on ID - by El Forum - 03-09-2009, 04:07 PM
Selecting records based on ID - by El Forum - 03-10-2009, 09:26 AM
Selecting records based on ID - by El Forum - 03-10-2009, 09:41 AM
Selecting records based on ID - by El Forum - 03-11-2009, 10:51 AM
Selecting records based on ID - by El Forum - 03-11-2009, 11:12 AM
Selecting records based on ID - by El Forum - 03-11-2009, 11:40 AM
Selecting records based on ID - by El Forum - 03-11-2009, 11:55 AM
Selecting records based on ID - by El Forum - 03-11-2009, 12:15 PM
Selecting records based on ID - by El Forum - 03-11-2009, 12:43 PM
Selecting records based on ID - by El Forum - 03-11-2009, 01:28 PM
Selecting records based on ID - by El Forum - 03-11-2009, 02:12 PM
Selecting records based on ID - by El Forum - 03-11-2009, 08:57 PM
Selecting records based on ID - by El Forum - 03-11-2009, 09:00 PM
Selecting records based on ID - by El Forum - 03-11-2009, 09:12 PM
Selecting records based on ID - by El Forum - 03-11-2009, 09:15 PM
Selecting records based on ID - by El Forum - 03-11-2009, 09:35 PM
Selecting records based on ID - by El Forum - 03-11-2009, 09:46 PM
Selecting records based on ID - by El Forum - 03-12-2009, 07:23 AM
Selecting records based on ID - by El Forum - 03-12-2009, 07:28 AM
Selecting records based on ID - by El Forum - 03-12-2009, 07:40 AM
Selecting records based on ID - by El Forum - 03-12-2009, 07:53 AM
Selecting records based on ID - by El Forum - 03-12-2009, 12:04 PM
Selecting records based on ID - by El Forum - 03-12-2009, 01:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB