MySQL group by without column name - mysql

I have a table contains user infos.
mysql> desc accounts;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| cid | int(11) | NO | PRI | NULL | auto_increment |
| username | text | YES | | NULL | |
| password | text | YES | | NULL | |
| mysignature | text | YES | | NULL | |
| is_admin | varchar(5) | YES | | NULL | |
+-------------+------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
Normal group by statement always follows by one or more column names like below:
select * from accounts group by cid;
Just an example, in general group by works with Aggregate functions like sum().
I have found some follows by an expression without column names which I cant understand:
mysql> select username from accounts group by now();
+----------+
| username |
+----------+
| admin |
+----------+
1 row in set (0.00 sec)
I am new in MySQL. How does this query work ?
Thanks.

In fact it's expression. It compares expression results instead of column values for the grouping.

now() function with the group by is not so effective.
It will always return the first row from the selected data.
You always need to specify the column name in the group by clause in query. Here are link of tutorial of group by.
It specify that which aggregate function you can use with group by.
now() with group by return that will select the data from the query and match the first and it will be first row.

Related

How select multiple value in one record in mysql?

I have a table with many column. one of this contain multiple argument, How can I select a field with one of this argument. for example my query is :
select name from product where product='carpet' and selling='new';
selling column contain 'new' , 'discounted', ..
You are looking for FIND_IN_SET
Returns a value in the range of 1 to N if the string str is in the
string list strlist consisting of N substrings. A string list is a
string composed of substrings separated by , characters
mysql> DESCRIBE products;
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| product | varchar(255) | YES | | NULL | |
| selling | varchar(255) | YES | | NULL | |
+---------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM products;
+----+---------+---------------------------+
| id | product | selling |
+----+---------+---------------------------+
| 1 | carpet | new,discounted,hello,worl |
| 2 | fork | used,other |
| 3 | plate | new |
| 4 | spoon | NULL |
+----+---------+---------------------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM products
-> WHERE product='carpet' AND FIND_IN_SET('new', selling) <> 0;
+----+---------+---------------------------+
| id | product | selling |
+----+---------+---------------------------+
| 1 | carpet | new,discounted,hello,worl |
+----+---------+---------------------------+
1 row in set (0.00 sec)
Like #BentCoder has rightly answered, MySQL has a dedicated function FIND_IN_SET() that returns the field index, if the value is found in a string containing comma-separated values.
SELECT * FROM products where product = 'carpet' and 'new' like concat('%',selling,'%');
Or you could also try this by adding commas to the left and right:
select * from products where product= 'carpet' and CONCAT(',', selling, ',') like '%,new,%'

MySql 5.6: escaping consecutive percent signs in like query

Consider 2 queries:
SELECT * FROM T1 WHERE NAME LIKE '%\%%';
SELECT * FROM T1 WHERE NAME LIKE '%\%\%%';
Assume that T1 has records where NAME is %, %%, or %%%.
I would expect the second query to return fewer results but it is including the record where T1.NAME = '%'! Is there a way to filter out that record using like query? Something like SELECT * FROM T1 WHERE NAME LIKE '%\%\%%' AND NAME <> '%'; is not what I am looking for.
mysql> explain table1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| text | varchar(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> explain select * from table1
-> where text like '%\%%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | table1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from table1
-> where text like '%\%\%%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | table1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
Here is my config if it helps:
[mysqld]
innodb_buffer_pool_size=402653184
innodb_log_file_size=262144000
innodb_log_buffer_size=8388608
max_allowed_packet=5241856
innodb_additional_mem_pool_size=20971520
I need to investigate further but it appears that the problem has something to do with the way that the database is being created:
create database testdb character set utf8 collate utf8_unicode_ci;
I was able to get the correct results when I create the database in a less specific way:
create database testdb;
Any idea why?
As far as I can tell, this appears to be a bug in mysql... it is sometimes impossible to search for consecutive percent signs using LIKE. (I was experiencing the same issue you described)
Still not sure what conditions are required to trigger the bug.
One workaround, however, would be to use REGEXP:
SELECT * FROM T1 WHERE NAME REGEXP "%%";

mysql column count doesn't match value count (redux): yes it does. or does it?

Is MySQL giving me grief because the nested SELECT in the insert statement uses the COUNT(*) function instead of selecting an actual column? So, what's the workaround?
Here's the story:
mysql> explain test;
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| language | varchar(50) | YES | | NULL | |
| count | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> SELECT languages.name, COUNT(*) AS `total` FROM languages JOIN events ON languages.id = events.language_id GROUP BY name HAVING total > 250 ORDER BY total DESC;
+-----------+-------+
| name | total |
+-----------+-------+
| Spanish | 60079 |
| Foochow | 2838 |
| Mandarin | 2396 |
| Russian | 1675 |
| Arabic | 1410 |
| Cantonese | 1358 |
| Korean | 736 |
| French | 531 |
| Punjabi | 426 |
| Urdu | 408 |
| Hebrew | 276 |
| Pashto | 255 |
+-----------+-------+
12 rows in set (0.00 sec)
mysql> INSERT INTO test (`language`,`count`) VALUES ((SELECT languages.`name`, COUNT(*) AS `total` FROM languages JOIN events ON languages.id = events.language_id GROUP BY name HAVING total > 250 ORDER BY total DESC));
ERROR 1136 (21S01): Column count doesn't match value count at row 1
thanks.
MySQL doesn't support this sort of multiple-column-returning subquery, so the error message you're seeing is because the VALUES clause only contains one subquery, which is perforce (in a sense) only one column.
To fix it, you can skip the VALUES syntax, and just write:
INSERT
INTO test (`language`,`count`)
SELECT languages.`name`, COUNT(*) AS `total`
FROM languages
JOIN events
ON languages.id = events.language_id
GROUP
BY name
HAVING total > 250
ORDER
BY total DESC
;
(See ยง13.2.5.1 "INSERT ... SELECT Syntax" in the MySQL 5.6 Reference Manual.)
INSERT INTO test (`language`,`count`)
should be
INSERT INTO test (language,count)

mysql union different databases but the same schema

as picture(phpmyadmin),I have tables in the database week1 the tables are now empty.
I have another databases with exactly the same sql schema w1moninside and w1monoutside
but they have values.
I want to union these two and insert them in week1
what should I do?
I inserted the picture just for clearance.unoin for one of the tables for example the first one, data
is enough for me
Thanks.
mysql> describe w1moninside.data;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| sid | int(10) unsigned | NO | PRI | NULL | |
| cid | int(10) unsigned | NO | PRI | NULL | |
| data_payload | text | YES | | NULL | |
+--------------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
Insert into week1.TableName (column list)
select column list from w1moninside.TableName where...
union
select column list from w1monoutside.TableName where
Insert into week1 (column list)
select column list from dbname.w1moninside where...
union
select column list from dbname.w1monoutside where..

mysql query issue

Let's assume I have the below table
mysql> desc countrylist;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| country | varchar(32) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.02 sec)
While querying the table, I always want 'USA' in the result whether or not the value is there in the table, in addition to other countries in the table. How do I handle that?
TIA.
James.
You can use UNION:
SELECT country FROM countrylist
UNION
SELECT 'USA'