Here is my reviews table: It's a table for generic reviews for a location with mostly ratings.
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| reviewid | int(11) | NO | PRI | NULL | auto_increment |
| createdate | int(11) | YES | | 0 | |
| locid | int(11) | YES | MUL | 0 | |
| clubid | int(11) | YES | | 0 | |
| userid | int(11) | YES | MUL | 0 | |
| legacyuser | int(11) | YES | | 0 | |
| author | varchar(100) | YES | | | |
| comment | varchar(4000) | YES | | | |
| rating | double | YES | | NULL | |
| status | int(11) | YES | | 1 | |
| likes | int(11) | YES | | 0 | |
| dislikes | int(11) | YES | | 0 | |
| spotlight | int(11) | YES | | 0 | |
| dancerquality | double | YES | | 0 | |
| pvtdancevalue | double | YES | | 0 | |
| hospitality | double | YES | | 0 | |
| dancerratio | double | YES | | 0 | |
| dancervariety | double | YES | | 0 | |
| covervalue | double | YES | | 0 | |
| drinksvalue | double | YES | | 0 | |
| foodvalue | double | YES | | 0 | |
| atmosphere | double | YES | | 0 | |
| soundsystem | double | YES | | 0 | |
| rebuttal | varchar(5000) | YES | | | |
+---------------+---------------+------+-----+---------+----------------+
If you look, you'll see these 3 columns that are important here:
covervalue (the cover price rating for this review)
drinksvalue (the drinks price rating for this review)
foodvalue (the food price rating for this review)
Here's what I need:
average of (avg of covervalue + avg of drinksvalue + avg of foodvalue)
My assumption is that the average of averages will give me the overall best value for pricing.
Here is my current query:
select avg(avg(foodvalue)+avg(drinksvalue)+avg(covervalue)) as avg from reviews where locid=3166;
Here is the response ERROR 1111 (HY000): Invalid use of group function
This clearly is not working or else I'd not be on here. Not quite sure how to do this?
Related
I have three tables
users table
+--------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| AccountNumber | varchar(50) | YES | | NULL | |
| DisplayName | varchar(255) | YES | MUL | NULL | |
| ParentAccountId | bigint | YES | MUL | NULL | |
| AccountTypeRef | bigint | YES | MUL | NULL | |
| PhoneNo | varchar(255) | YES | | NULL | |
| FaxNo | varchar(50) | YES | | NULL | |
| Website | varchar(200) | YES | | NULL | |
| Email | varchar(100) | YES | | NULL | |
| StateRef | bigint | YES | MUL | NULL | |
| Address | longtext | YES | | NULL | |
| PostalCode | varchar(50) | YES | | NULL |
products table
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| ProductId | bigint | YES | MUL | NULL | |
| PackName | varchar(255) | YES | | NULL | | |
| Price | double | YES | | NULL | |
| OnlineInfoAddress | varchar(255) | YES | | NULL | |
| Description | longtext | YES | | NULL | |
| IsDelete | tinyint | YES | | NULL | |
| CreateUserId | bigint | YES | | NULL | |
| CreateDate | datetime | YES | | NULL | |
| EditDate | datetime | YES | | NULL | |
| EditUserId | bigint | YES | | NULL | |
| Tag | int | YES | | NULL | |
| Ref | varchar(50) | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
users_buy table
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| Id | bigint | NO | PRI | NULL | auto_increment |
| users_id | bigint | YES | MUL | NULL | |
| product_id | bigint | YES | MUL | NULL | |
| BuyDate | datetime | YES | | NULL | |
| CountBuy | int | YES | | NULL | |
| TotalPrice | double | YES | | NULL | |
| Description | longtext | YES | | NULL | |
| invoiceNumber | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
I want to get users who did not buy a specific product
for example, user number 20 bought 5 product with ids [1,2,3,4,5]
and user number 19 bought 3 product with ids [1,2,3]
And user number 18 bought 2 product with ids [1,3]
I want to get users who did not buy any product with the id 5
ie I want user 18 and 19
How can I do this?
This will list of all users that did not buy product id 5:
SELECT *
FROM users
WHERE
Id NOT IN (
SELECT users_id
FROM users_buy
WHERE product_id = 5
)
One more solution used LEFT JOIN:
SELECT users.*
FROM users
LEFT JOIN users_buy
ON users_buy.users_id = users.id AND users_buy.product_id = 5
WHERE users_buy.user_id IS NULL;
The above query will return records from users table have not matched rows in users_buy table with product_id 5
I'm trying to figure out why results returned via a MySQL terminal query don't match those returned by a MySQL query executed by PHP.
Here's the MySQL used in the shell:
SELECT * FROM SANKEY_NODE AS n
LEFT OUTER JOIN TYPE_DETAIL as td
ON n.TYPE_DETAIL_ID = td.TYPE_DETAIL_ID
LEFT OUTER JOIN GRAPH_TYPE as t
ON td.GRAPH_TYPE_ID = t.GRAPH_TYPE_ID
WHERE CHART_ID = 39;
Here's the PHP:
function get_nodes_by_chart_id($con, $chart_id) {
$sql = 'SELECT * FROM SANKEY_NODE AS n
LEFT OUTER JOIN TYPE_DETAIL as td
ON n.TYPE_DETAIL_ID = td.TYPE_DETAIL_ID
LEFT OUTER JOIN GRAPH_TYPE as t
ON td.GRAPH_TYPE_ID = t.GRAPH_TYPE_ID
WHERE CHART_ID = '.$chart_id.';';
$result = mysqli_query($con, $sql);
return results_to_array($result);
}
function results_to_array($results) {
$rows = array();
while($row = mysqli_fetch_assoc($results)) {
$rows[] = $row;
}
return $rows;
}
Both queries return a dozen results, however, they differ in their representation of three results. Those three results are special because they have a TYPE_DETAIL_ID value in the SANKEY_NODE table that is not present in the TYPE_DETAIL table.
In the MySQL shell, the TYPE_DETAIL_ID value for the three affected nodes is displayed, whereas in the results returned by PHP, the TYPE_DETAIL_ID value is null. Does anyone know what might cause this difference? I'd be very grateful for any insight others can offer!
Table Structures
mysql> describe SANKEY_NODE;
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| NODE_NAME | varchar(100) | NO | | NULL | |
| NODE_PARENT | varchar(20) | YES | | NULL | |
| CHART_ID | int(11) | NO | PRI | NULL | |
| NODE_TYPE | varchar(100) | NO | | NULL | |
| TYPE_DETAIL_ID | varchar(20) | NO | PRI | NULL | |
+----------------+--------------+------+-----+---------+-------+
mysql> describe TYPE_DETAIL;
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| COMPANY_ID | int(11) | NO | | NULL | |
| GRAPH_TYPE_ID | int(11) | NO | PRI | NULL | |
| TYPE_DETAIL_CD | varchar(20) | NO | PRI | NULL | |
| TYPE_DETAIL_NAME | varchar(100) | NO | | NULL | |
| TYPE_DETAIL_DESC | varchar(200) | YES | | NULL | |
| TYPE_DETAIL_ID | int(11) | NO | | NULL | |
| TYPE_IMAGE_ID | int(11) | YES | | NULL | |
| ACTIVE_FLAG | bit(1) | NO | | NULL | |
+------------------+--------------+------+-----+---------+-------+
mysql> describe GRAPH_TYPE;
+----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+----------------+
| COMPANY_ID | int(11) | NO | PRI | NULL | |
| GRAPH_TYPE_ID | int(11) | NO | UNI | NULL | auto_increment |
| TYPE_CD | varchar(20) | NO | PRI | NULL | |
| TYPE_NAME | varchar(100) | NO | | NULL | |
| TYPE_COLOR | varchar(50) | NO | | NULL | |
| TYPE_HIGHLIGHT_COLOR | varchar(50) | NO | | NULL | |
| ACTIVE_FLAG | bit(1) | NO | | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
Comment Responses
#Cedric, running the query with just the first JOIN yields the same results. The three values whose TYPE_DETAIL_ID is present in SANKEY_NODE but not in TYPE_DETAIL_ID have a defined TYPE_DETAIL_ID in the shell results but not in the results via PHP (see results below). As for syntax, I'm a fan of the philosophy that "explicit is better than implicit".
#jcaron, the TYPE_DETAIL_ID values are either sequences of integers or ascii strings, e.g.:
mysql> SELECT * FROM SANKEY_NODE AS n LEFT OUTER JOIN TYPE_DETAIL as td ON n.TYPE_DETAIL_ID = td.TYPE_DETAIL_ID WHERE CHART_ID = 3;
+--------------------------+-------------+----------+-------------+----------------+------------+---------------+----------------+--------------------+--------------------+----------------+---------------+-------------+
| NODE_NAME | NODE_PARENT | CHART_ID | NODE_TYPE | TYPE_DETAIL_ID | COMPANY_ID | GRAPH_TYPE_ID | TYPE_DETAIL_CD | TYPE_DETAIL_NAME | TYPE_DETAIL_DESC | TYPE_DETAIL_ID | TYPE_IMAGE_ID | ACTIVE_FLAG |
+--------------------------+-------------+----------+-------------+----------------+------------+---------------+----------------+--------------------+--------------------+----------------+---------------+-------------+
| CRD | SYS | 3 | System | 101004 | 7777 | 1 | CRD | Charles River | Charles River | 101004 | NULL | |
| FactSet | SYS | 3 | System | 101012 | 7777 | 1 | FACTSET | Factset | Factset | 101012 | NULL | |
| MSCI | SYS | 3 | System | 101016 | 7777 | 1 | RISKMETRICS | MSCI RiskWorld | MSCI RiskWorld | 101016 | NULL | |
| Trade Execution | FUN | 3 | Function | 109007 | 7777 | 9 | TE | Trade Execution | Trade Execution | 109007 | NULL | |
| Portfolio Mgmt | FUN | 3 | Function | 109003 | 7777 | 9 | PM | Portfolio Mgmt | Portfolio Mgmt | 109003 | NULL | |
| Performance & Risk | FUN | 3 | Function | 109002 | 7777 | 9 | PMR | Performance & Risk | Performance & Risk | 109002 | NULL | |
| Operations | FUN | 3 | Function | 109006 | 7777 | 9 | OPS | Operations | Operations | 109006 | NULL | |
| Decision Making | FUN | 3 | Function | 109001 | 7777 | 9 | DM | Decision Making | Decision Making | 109001 | NULL | |
| Compliance | FUN | 3 | Function | 109005 | 7777 | 9 | COMP | Compliance | Compliance | 109005 | NULL | |
| Portfolio Rebalance | SFUN | 3 | SubFunction | 201091 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Position Reconciliation | SFUN | 3 | SubFunction | 201092 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| PreTrade Compliance | SFUN | 3 | SubFunction | 201096 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Step-outs | SFUN | 3 | SubFunction | 201109 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Trade Matching | SFUN | 3 | SubFunction | 201125 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Trade Settlement | SFUN | 3 | SubFunction | 201129 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Functions | | 3 | Function | FUN | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| SubFunction | | 3 | SubFunction | SFUN | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| Systems | | 3 | System | SYS | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------------------------+-------------+----------+-------------+----------------+------------+---------------+----------------+--------------------+--------------------+----------------+---------------+-------------+
18 rows in set, 225 warnings (0.00 sec)
Here is an easy workaround, specify the table name for each column :
SELECT SANKEY_NODE.TYPE_DETAIL_ID FROM SANKEY_NODE AS n
That way, you will be sure to have the data that you expect
I was bee-bopping along writing code and testing it when I hit this error.
update products set enable_flag='Y' where prod_id=31745
ERROR # 214 : (1054)
Unknown column 'enable_flag' in 'field list'
The "enable_flag" field is listed in the table. I have tried several variants of the above SQL command. I've put backticks around "products", around "enable_flag", I've tried retyping the command - all to no avail. I even thought that maybe I'd exceeded the length limit of 65,536 characters - but no. So I came here, read up on other's having a similar problem but those were all "You mistyped X" or "You left off the name of the table" and so forth. This has got me a bit stumped. I'm using MySQL (latest version), PHP 5.4.12, and before yesterday I have mainly worked with the first half of the table. So now I'm starting to work with the second half and this happens. Also, enable_flag is column 52. I know you can have 4096 columns. However, anything from column 52 on can not be updated, inserted, etc....
Here is the table "products" dump:
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| prod_id | bigint(20) | NO | PRI | NULL | auto_increment |
| sku | varchar(40) | YES | | NULL | |
| upc | varchar(40) | YES | | NULL | |
| title | text | YES | | NULL | |
| long_desc | mediumtext | YES | | NULL | |
| cat_id | int(11) | YES | | NULL | |
| size | varchar(80) | NO | | NULL | |
| color | varchar(80) | NO | | NULL | |
| model | varchar(80) | YES | | NULL | |
| quantity | int(11) | YES | | NULL | |
| stock_status_id | int(11) | YES | | NULL | |
| image_id | bigint(20) | YES | | NULL | |
| mfg_id | int(11) | YES | | NULL | |
| shipping | decimal(10,2) | YES | | NULL | |
| price | decimal(10,2) | YES | | NULL | |
| points | decimal(10,2) | YES | | NULL | |
| tax_class_id | int(11) | YES | | NULL | |
| weight | decimal(10,2) | YES | | NULL | |
| width | decimal(10,2) | YES | | NULL | |
| length | decimal(10,2) | YES | | NULL | |
| height | decimal(10,2) | YES | | NULL | |
| viewed | int(11) | YES | | NULL | |
| date_used | datetime | YES | | NULL | |
| date_added | datetime | YES | | NULL | |
| date_modified | datetime | YES | | NULL | |
| date_available | datetime | YES | | NULL | |
| sellor_id | int(11) | YES | | NULL | |
| sellor_product_id | varchar(80) | YES | | NULL | |
| state_codes | text | YES | | NULL | |
| ground_ship_only | varchar(1) | YES | | n | |
| adult_sig_req | varchar(1) | YES | | n | |
| no_drop_ship | varchar(1) | YES | | n | |
| priority | int(3) | YES | | NULL | |
| unlimited_stock | varchar(1) | YES | | NULL | |
| reorder_level | int(11) | YES | | NULL | |
| license_key | text | YES | | NULL | |
| store_cost | decimal(10,2) | YES | | NULL | |
| msrp | decimal(10,2) | YES | | NULL | |
| handling_cost | decimal(10,2) | YES | | NULL | |
| min_order_quantity | int(11) | YES | | NULL | |
| max_order_quantity | int(11) | YES | | NULL | |
| weight_major | decimal(10,2) | YES | | NULL | |
| weight_minor | decimal(10,2) | YES | | NULL | |
| warehouse | text | YES | | NULL | |
| shipping_msg | text | YES | | NULL | |
| category_special | varchar(1) | YES | | NULL | |
| homepage_special | varchar(1) | YES | | NULL | |
| shipping_option | text | YES | | NULL | |
| product_type | text | YES | | NULL | |
| related_products | mediumtext | YES | | NULL | |
| product_url | text | YES | | NULL | |
| enable_flag | varchar(1) | YES | | NULL | |
| purchasable | varchar(1) | YES | | NULL | |
| price_msg | text | YES | | NULL | |
| in_stock_msg | text | YES | | NULL | |
| out_of_stock_msg | text | YES | | NULL | |
| variations | text | YES | | NULL | |
| attributes | text | YES | | NULL | |
| delete_flag | varchar(1) | YES | | NULL | |
| short_desc | text | YES | | NULL | |
| cross_ref | text | YES | | NULL | |
| model_no | varchar(40) | YES | | NULL | |
| pkg_qty | int(11) | YES | | 1 | |
| military_spec | varchar(40) | YES | | NULL | |
| grips | varchar(80) | YES | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
In this case, the problem was the database became confused on what I actually wanted to do. There is only one "products" table so the database should not have become confused - but it did. The solution is to add the database's name on to the command so there isn't any confusion. So the command should have been:
update store.products where enable_flag='Y' where prod_id=31750
By placing the "store" part on to the command, the database.table connection is then established and the field is found.
I suspect that your enable_flag might have other characters. You can figure this out using information_schema.columns:
select concat('"', c.column_name, '"')
from information_schema.columns c
where table_name = 'products' and column_name like '%enable%';
If you find extra characters, you will probably need backticks.
I have a a mysql db with myisam engine. Along with many other table I have this table "et" which has around 8137037 records.
I have created indexes (individual index of column hname and pnum, it did not help much later created joint index of hname and pnum and it help execute within a second)such that queries as follows
select st from et where hname='name' and pnum='1' limit 1
execute fast (with in a second) but the problem is I must execute this query
select st from et where hname='name' and pnum='1' order by id limit 1
where id is the primary key of the table and this query sometimes take 145 seconds :(
how can i resolve this issue?
table structure
mysql> describe et;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| client_id | int(11) | YES | MUL | NULL | |
| down_id | bigint(20) | YES | MUL | NULL | |
| port_index | int(11) | YES | | NULL | |
| date1 | bigint(20) | YES | MUL | NULL | |
| username | varchar(50) | YES | | NULL | |
| hname | varchar(80) | YES | MUL | NULL | |
| pnum | varchar(10) | YES | MUL | NULL | |
| status | int(3) | YES | MUL | NULL | |
| department | int(2) | YES | MUL | NULL | |
| comments | text | YES | | NULL | |
| issue | int(1) | YES | MUL | NULL | |
+------------+-------------+------+-----+---------+----------------+
12 rows in set (0.06 sec)
Indexes
mysql> show indexes from et;
+-----+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
|Table| Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| et | 0 | PRIMARY | 1 | id | A | 8137037 | NULL | NULL | | BTREE | |
| et | 1 | status | 1 | status | A | 12 | NULL | NULL | YES | BTREE | |
| et | 1 | down_id | 1 | down_id | A | 4068518 | NULL | NULL | YES | BTREE | |
| et | 1 | issue_idx | 1 | issue | A | 8137037 | NULL | NULL | YES | BTREE | |
| et | 1 | hname_idx | 1 | hname | A | 283 | NULL | NULL | YES | BTREE | |
| et | 1 | pname_idx | 1 | pnum | A | 136 | NULL | NULL | YES | BTREE | |
| et | 1 | clientid_idx | 1 | client_id | A | 8137037 | NULL | NULL | YES | BTREE | |
| et | 1 | date1_idx | 1 | date1 | A | 8137037 | NULL | NULL | YES | BTREE | |
| et | 1 | department_idx | 1 | department | A | 2712345 | NULL | NULL | YES | BTREE | |
| et | 1 | hp_idx | 1 | hname | A | 283 | NULL | NULL | YES | BTREE | |
| et | 1 | hp_idx | 2 | pnum | A | 4834 | NULL | NULL | YES | BTREE | |
+-----+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
11 rows in set (0.06 sec)
Explain Plan
mysql> explain select status from et where hname='mmah' and port_num='1' limit 1;
+----+-------------+------------+------+----------------------------------+--------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+----------------------------------+--------------+---------+-------------+------+-------------+
| 1 | SIMPLE | error_trap | ref | hname_idx,pname_idx,hostport_idx | hostport_idx | 96 | const,const | 37 | Using where |
+----+-------------+------------+------+----------------------------------+--------------+---------+-------------+------+-------------+
1 row in set (0.02 sec)
mysql> explain select status from et where host_name='mmah' and pnum='1' order by id desc limit 1;
+----+-------------+------------+------+----------------------------------+--------------+---------+-------------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+----------------------------------+--------------+---------+-------------+------+-----------------------------+
| 1 | SIMPLE | error_trap | ref | hname_idx,pname_idx,hostport_idx | hostport_idx | 96 | const,const | 37 | Using where; Using filesort |
+----+-------------+------------+------+----------------------------------+--------------+---------+-------------+------+-----------------------------+
1 row in set (0.00 sec)
Have you tried creating an index of (id, hname,pnum)?
If your index includes and is already ordered by ID then that effectively removes that concern.
There is no reason you cannot include ID in another index, just because it is also the primary.
Create an index in the table that contains hname and pnum.
This will cause MYSQL to create a shadow table, which only contains those 2 parameters, and whenever those 2 parmaters are used in a where, it looks in that shadow table, and finds them very fast.
I'm trying to get the number of games that summoners in the queue have played today. I have tried several queries. The closest I have gotten is:
SELECT
s.summoner,
s.wins,
COUNT(*) as playedToday
FROM summoners s
LEFT JOIN teams_players tp ON tp.summoner_id = s.summoner
LEFT JOIN teams t ON t.id = tp.team_id
LEFT JOIN matches b ON t.id = b.blue_id
WHERE
b.played_on = DATE(NOW())
s.in_queue;
But that doesn't give the count per-user. It just gets the overall count.
None of the tables have any data on any of the summoners. What I'd like is something like:
+-----------------+------+-------------+
| summoner | wins | gamesPlayed |
+-----------------+------+-------------+
| DotAliscious | 353 | 0 |
| Kraator | 440 | 0 |
| Nammon | 667 | 0 |
| VictorousSecret | 843 | 0 |
| Canas | 544 | 0 |
| Sprognak | 502 | 0 |
| Ghostilocks | 808 | 0 |
| b0b d0e | 224 | 0 |
| Metroshica | 339 | 0 |
| RubenatorX | 478 | 0 |
+-----------------+------+-------------+
I can't think how to express this problem generically, which is why I haven't found a solution.
My data set. Note that teams.id matches up with matches.blue_id and matches.purple_id.
mysql> DESCRIBE summoners;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| icon | int(11) | NO | | 0 | |
| summoner | varchar(32) | NO | UNI | NULL | |
| skype | varchar(32) | NO | | NULL | |
| email | varchar(32) | NO | | NULL | |
| in_queue | tinyint(1) | NO | | 0 | |
| wins | int(11) | NO | | 0 | |
| level | int(11) | NO | | 1 | |
| lan_wins | int(11) | NO | | 0 | |
| played_today | int(11) | NO | | 0 | |
+--------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
mysql> DESCRIBE matches;
+-----------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| blue_id | int(11) | NO | MUL | NULL | |
| purple_id | int(11) | NO | MUL | NULL | |
| status | varchar(32) | NO | | display | |
| played_on | datetime | NO | | 2012-04-06 13:53:55 | |
+-----------+-------------+------+-----+---------------------+----------------+
5 rows in set (0.00 sec)
mysql> DESCRIBE teams;
+---------+-------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| outcome | varchar(32) | NO | | incomplete | |
+---------+-------------+------+-----+------------+----------------+
2 rows in set (0.00 sec)
mysql> DESCRIBE teams_players;
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| team_id | int(11) | NO | MUL | NULL | |
| summoner_id | int(11) | NO | MUL | NULL | |
+-------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
You're going to want to use a subselect to get the count of records that meet the condition, and that will return you a 0 if the condition is false. Something along the lines of
(SELECT COUNT(*) FROM matches b WHERE b.blue_id = t.id AND b.played_on = DATE(NOW())) AS playedToday
and you will want to remove the LEFT JOIN to the matches table and the WHERE clause checking the date.
Your played_on is datetime and you are comparing it with date maybe that's your problem. YOu should try:
WHERE
DATE(b.played_on) = DATE(NOW())
Also what do you mean by s.in_queue? Did you forget the AND? Do you mean
WHERE
DATE(b.played_on) = DATE(NOW())
AND
s.in_queue;