Hi There -
Thanks for giving this a look. This is spinning my brain in a bunch of different directions. I have two tables. Their common column is pid.
table_1
item | pid | count
soap | 001 | 4
shoe | 002 | 2
cups | 003 | 3
soap | 004 | 2
where count is the number of items associated with a pid (purchase id)
table_2
pid | uid | type
002 | 1 | amex
003 | 2 | visa
004 | 1 | amex
001 | 3 | visa
where uid is the user id and type is the type of purchase.
What I'm trying to do is get a total of the number of items a user has purchased of a certain type.
In the above example, I need to figure out how to join the two tables and extract the total count of soap that user 1 has purchased using amex, which would be 6.
Here's what I've worked out. MySQL just laughs at me.
SELECT uid, count WITH ROLLUP
FROM table_1 JOIN table_2 ON (pid = pid)
WHERE uid = $uid and type=amex;
'WITH ROLLUP' is supposed to create a new row with a total on it. Honestly. I have no idea how to proceed from here. I'm really not grokking how to extract the rollup number from that join.
Same with this:
SELECT uid, count SUM(amount) AS total_amount
FROM table_1 JOIN table_2 ON (pid = pid)
WHERE uid = $uid and type=amex;
On a really basic level, I don't really get the extraction of data from a join or a computation. Put them both together and I'm really lost.
I'd be grateful for any nudges in the right direction or clear explanations of how to get stuff out of a join. I also don't really understand how to use 'AS' and it's very difficult to search for.
Thanks!