ditto
ditto

Reputation: 6277

Combining two queries that already use JOIN's

I have two separate queries that I'm wanting to combine.

SELECT e.id, e.measurement, e.name, weight, time, reps, distance, exerciseid, DATE(date) as date
FROM `users exercises` LEFT JOIN exercises as e ON e.id = exerciseid
WHERE `userid` = '24' AND `date` < now()
ORDER BY date DESC LIMIT 100

SELECT f.id, f.name, f.calories, f.protein, f.carbohydrate, DATE(date) as date, value, meal, unit
FROM `users foods` LEFT JOIN foods as f ON f.id = foodid
WHERE `userid` = '24' AND `date` < now()
ORDER BY date desc LIMIT 100

Outputs:

Array
(
    [id] => 489
    [measurement] => 2
    [name] => Dumbbell Stability Ball Press
    [weight] => 20
    [time] => 
    [reps] => 20
    [distance] => 
    [exerciseid] => 489
    [date] => 2013-08-01
)

Array
(
    [id] => 7
    [name] => Cheese, camembert
    [calories] => 300
    [protein] => 19.8
    [carbohydrate] => 0.46
    [date] => 2013-09-20
    [value] => 23
    [meal] => 3
    [unit] => 2
)

Is it possible to combine these whilst keeping the same output?

EDIT: Sorry, I wasn't clear. I do not want to join these queries together, I'm not after this:

Array
(
    [id] => 672
    [measurement] => 1
    [name] => Ab Wheel (standing)
    [weight] => 
    [time] => 
    [reps] => 5
    [distance] => 
    [exerciseid] => 672
    [fid] => 23
    [fname] => Cheese, gruyere
    [calories] => 413
    [protein] => 29.81
    [carbohydrate] => 0.36
    [value] => 54
    [meal] => 1
    [unit] => 
)

I want to keep the arrays the same as they were from the original, but instead of using two queries, I want to use just the one. If possible.

Upvotes: 1

Views: 94

Answers (3)

ditto
ditto

Reputation: 6277

Here is my solution:

(
    SELECT 'e' AS type,
            exerciseid as id,
            DATE(date) as date,
            e.name,
            weight,
            time,
            reps,
            distance,
            e.measurement,

            null as value,
            null as meal,
            null as unit,
            null as calories,
            null as protein,
            null as carbohydrate

    FROM `users exercises`
    LEFT JOIN exercises as e ON e.id = exerciseid

    WHERE `userid` = $user->id AND `date` < now()
    ORDER BY date desc
)

UNION ALL

(
    SELECT 'f' AS type,
            foodid as id,
            DATE(date) as date,
            f.name,
            null as weight,
            null as time,
            null as reps,
            null as distance,
            null as measurement,

            value,
            meal,
            unit,
            f.calories,
            f.protein,
            f.carbohydrate

    FROM `users foods`
    LEFT JOIN foods as f ON f.id = foodid

    WHERE `userid` = $user->id AND `date` < now()
    ORDER BY date desc
)

Upvotes: 0

ngneema
ngneema

Reputation: 444

Assuming some things here, but you can join all of these tables together if they share the userid

SELECT e.id, e.measurement, e.name, weight, time, reps, distance, exerciseid, 
  ue.DATE(date) as date, f.id, f.name, f.calories, f.protein, f.carbohydrate, uf.value,
  uf.meal, uf.unit
FROM `users exercises` 
LEFT JOIN exercises as e ON e.id = exerciseid
LEFT JOIN `user foods` as uf ON uf.userid = e.userid
LEFT JOIN `foods` as f ON f.userid = e.userid
WHERE `userid` = '24' AND `date` < now()
ORDER BY date DESC LIMIT 100

Upvotes: 1

Stefan M
Stefan M

Reputation: 889

Of course you can still join more tables, just make sure to give the tables the correct identification parameters. Untested longshot:

SELECT e.id, e.measurement, e.name, weight, time, reps, distance, exerciseid, DATE(date) as date,
f.id, f.name, f.calories, f.protein, f.carbohydrate, DATE(date) as date, value, meal, unit
FROM `users exercises`
LEFT JOIN exercises as e ON
    e.id = exerciseid
INNER JOIN `users foods` ON
    `users foods`.userid = `users exercises`.userid
LEFT JOIN foods as f ON
    f.id = foodid
WHERE `users exercises`.`userid` = '24' AND `users exercises`.`date` < now()
ORDER BY date DESC LIMIT 100

I assume that it suffices that the two tables are linked by the userid, but as I don't know the content, I can't be sure about the date.

Upvotes: 0

Related Questions