Welcome Guest, Not a member yet? Register   Sign In
Concatenating Query Results?
#1

[eluser]Ray Julich[/eluser]
I'm creating a resource (laptops, projectors, cell phones, PDAs) reservation system with Code Igniter. I have 1 "reservations" table that contains a person's reservation information (pickup date, return date, username, resource_id, etc.) and seperate tables for each type of resource.

When a person is view the reservation system in their browser and is looking at the reservations by date, it shows the information about each reservation - pickup date, return date, username, resource_type, and the resource's name (instead of the resource_id). Say I want to select and display the reservations for July 26, 2010.

"SELECT * FROM reservations, laptops WHERE laptops.serial = reservations.resource_id ORDER BY reservations.pickup_date"

This give me a list of all the laptop reservations, but I also need to display the reservations for projectors, cell phones, and PDAs. Can I do the same SQL statement for each type of resource and concatentate the results from each query?

$results = $laptop_results . $projector_results . $cell_phone_results . $pda_results;

A successful query returns an array of objects. If $results is successfully created, then I will have an array of objects that will need to be sorted by the "pickup_date" field. Is it possible to do this sort?

Or is there a magical SQL statement ;-) that will match up the resource_id for each reservations from whatever resource_type it is?
#2

[eluser]danmontgomery[/eluser]
http://dev.mysql.com/doc/refman/5.0/en/union.html
#3

[eluser]Ray Julich[/eluser]
That's what I need. Thank you. MySQL must be magical. :-)
#4

[eluser]PHPexpert[/eluser]
Ray,

Looks like UNION in MySQl is what you would call Constanation in MySQL. Remember the two table structure have to be the same or at least have the same datasource, ig you know what I mean. See below.

(SELECT * FROM tableOne WHERE item1=10 AND item2=1)
UNION
(SELECT * FROM tableTwo WHERE item1=11 AND item2=2)
ORDER BY a LIMIT 10;

Hope this will help you,
#5

[eluser]Ray Julich[/eluser]
Yes, this solution will work for me.




Theme © iAndrew 2016 - Forum software by © MyBB