I am having trouble matching in Mysql. Pretty simple, all right, and most of the tutorials that I find rarely go beyond typical ones:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
What I'm trying to pull from my database is the following (I will try to explain this without any prerequisites on our db):
Get a list of customers belonging to a certain reputation, and the total amount spent last month (in one column) and the amount spent during the month to date, in another column.
As a result, it looks something like this:
ID | NAME | PREV MONTH | CUR MONTH 1 | foobar | £2300 | £1200 2 | barfoo | £1240 | £500
The query that I use to get the first piece of data is as follows:
SELECT c.id,c.name, SUM(co.invoicetotal) as total FROM customers as c JOIN customerorders as co on co.customer_id = c.id WHERE c.salesrep_id = 24 AND co.orderdate BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE() GROUP by c.id order by total desc
DATE_SUB can be replaced with actual dates as php variables will be here in the end. As an example, this just gives me reliable data.
This gives me, for example:
ID | NAME | TOTAL 1 | foobar | £2300 2 | barfoo | £1240
So, ideally, my subquery would be exactly the same query, but with modified dates. I keep getting error #1242 - Subquery returns more than 1 row .
Any suggestions or tips please?
Thanks in advance. Rob