I have a question about inserting row in an table which is already created.
This is my table :
mysql> describe llx_document_model ;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| rowid | int(11) | NO | PRI | NULL | auto_increment |
| nom | varchar(50) | YES | MUL | NULL | |
| entity | int(11) | NO | | 1 | |
| type | varchar(20) | NO | | NULL | |
| libelle | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
If I print the table :
mysql> select * from llx_document_model ;
+-------+----------+--------+-------------------+---------+-------------+
| rowid | nom | entity | type | libelle | description |
+-------+----------+--------+-------------------+---------+-------------+
| 1 | standard | 1 | deplacement | NULL | NULL |
| 7 | soleil | 1 | ficheinter | NULL | NULL |
| 13 | rouget | 1 | shipping | NULL | NULL |
| 14 | typhon | 1 | delivery | NULL | NULL |
| 16 | aurore | 1 | supplier_proposal | NULL | NULL |
| 17 | muscadet | 1 | order_supplier | NULL | NULL |
| 18 | baleine | 1 | project | NULL | NULL |
| 19 | einstein | 1 | order | NULL | NULL |
| 21 | azur | 1 | propal | NULL | NULL |
| 23 | strato | 1 | contract | strato | NULL |
| 32 | crabe | 1 | invoice | crabe | NULL |
+-------+----------+--------+-------------------+---------+-------------+
11 rows in set (0.00 sec)
I want to add a row, so I write :
mysql> INSERT INTO llx_document_model
-> VALUES(NULL, moriba, 1, invoice, moriba, NULL);
But I get this error :
ERROR 1054 (42S22): Unknown column 'moriba' in 'field list'
Do you have some idea about my problem ? I don't see really where I made an error.
Thank you by advance !
string should be enclosed by (')single quotes
mysql> INSERT INTO llx_document_model VALUES(NULL, 'moriba', 1, 'invoice', 'moriba', NULL);
Related
I have table transaksi:
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(150) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| type | int(11) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
and have data (I fill in the data with insert into transaksi (name,price,type) select name,price,type from store)
+-----------+--------------------------+---------+-------+
| id | name | price | Type |
+-----------+--------------------------+---------+-------+
| NULL | Pants | 79 | 9 |
| NULL | Cup | 38 | 7 |
| NULL | Shoes | 21 | 1 |
| NULL | Hat | 11 | 5 |
| NULL | Pulpen | 39 | 2 |
+___________|__________________________|_________|_______|
How to fill "NULL" data at once in ID ?
I have been working with SQL for a very long time, but this is the first time I've seen it.
I have a table contacts:
+------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| client_id | varchar(191) | YES | MUL | NULL | |
| client_status | int(11) | YES | | NULL | |
| lastname | varchar(191) | YES | | NULL | |
| firstname | varchar(191) | YES | | NULL | |
| patronymic | varchar(191) | YES | | NULL | |
| date_of_birth | date | YES | MUL | NULL | |
| sex | int(11) | YES | | NULL | |
| state | int(11) | YES | MUL | NULL | |
| phone_mobile | varchar(191) | YES | MUL | NULL | |
| phone_home | varchar(191) | YES | | NULL | |
| adr_city_type | varchar(191) | YES | | NULL | |
| adr_city | varchar(191) | YES | | NULL | |
| adr_apt | varchar(191) | YES | | NULL | |
| adr_street_num | varchar(191) | YES | | NULL | |
| adr_street | varchar(191) | YES | | NULL | |
| adr_street_type | varchar(191) | YES | | NULL | |
| adr_district | varchar(191) | YES | | NULL | |
| adr_postcode | varchar(191) | YES | | NULL | |
| email | varchar(191) | YES | MUL | NULL | |
| comment | text | YES | | NULL | |
+------------------+--------------+------+-----+---------+-------+
I have a simple query:
select client_id, lastname, firstname, phone_mobile, email
from contacts
where `client_id`="8011000000000005263"
limit 1;
When I execute it, I get the following result:
Empty set (0.00 sec)
I know that this identifier is a database and I decided to experiment with the query, see the result below.
2.
mysql> select client_id, lastname, firstname, phone_mobile, email from contacts where `client_id` like "%8011000000000005263%" limit 1;
Empty set (1.74 sec)
3.
mysql> select client_id, lastname, firstname, phone_mobile, email from contacts whereclient_id=8011000000000005263 limit 1;
+-------------------+----------------------+-----------+---------------+-------+
| client_id | lastname | firstname | phone_mobile | email |
+-------------------+----------------------+-----------+---------------+-------+
| 8011000000000004655| OSTROVKAYA | YANA | +111111111111| NULL |
+-------------------+----------------------+-----------+---------------+-------+
1 row in set (0.68 sec)
4.
mysql> select client_id, lastname, firstname, phone_mobile, email from
contacts where `client_id`= 8011000000000005263;
234 rows in set (0.83 sec) - WTF ???
5.
mysql> select client_id, lastname, firstname, phone_mobile, email from
contacts use index (client_id) where `client_id`=8011000000000005263;
234 rows in set (0.90 sec) - WTF ???
6.
mysql> select client_id, lastname, firstname, phone_mobile, email from contacts use index (client_id) where client_id in (8011000000000005263) group by id_client;
234 rows in set (1.94 sec) - WTF ???
I want to provide an explanation for the last request:
mysql> explain select client_id, lastname, firstname, phone_mobile, email from contacts use index (client_id) where client_id in (8011000000000005263) group by id_client;
+----+-------------+--------------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | contacts | NULL | index | client_id | client_id | 576 | NULL | 1183745 | 10.00 | Using where |
+----+-------------+--------------+------------+-------+---------------+-----------+---------+------+---------+----------+-------------+
1 row in set, 3 warnings (0.00 sec)
I can not understand why a simple query is not executed correctly. And how do I get the right result.
Moreover, this request is placed on the MySQL server (((
When we will be do communicate with the auxiliary table, then the request hangs for 20-30 minutes.
To be fully aware of what is happening I will provide table indexes:
+--------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| contacts | 1 | id | 1 | id | A | 1177634 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | client_id | 1 | client_id | A | 1136884 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 1 | email | A | 194844 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 2 | phone_mobile | A | 1165372 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 3 | date_of_birth | A | 1081845 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | combo | 4 | state | A | 1148369 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | email | 1 | email | A | 197052 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | phone_mobile | 1 | phone_mobile | A | 1127414 | NULL | NULL | YES | BTREE | | |
| contacts | 1 | state | 1 | state | A | 2 | NULL | NULL | YES | BTREE | | |
+--------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
9 rows in set (0.02 sec)
Please, could not experienced developers explain to me this phenomenon of the behavior of this language.
Thank you so much! All clean code :)
Parent Table,
mysql> desc sattool_testing;
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| circuit_id | varchar(30) | NO | | NULL | |
| coff_id | varchar(100) | NO | | NULL | |
| result | int(11) | YES | | NULL | |
| result_details | text | NO | | NULL | |
| details | longtext | NO | | NULL | |
| reverse_response | varchar(100) | NO | | NULL | |
| start_date | datetime | NO | | NULL | |
| end_date | datetime | YES | | NULL | |
| Modules | varchar(100) | YES | | NULL | |
| rehit | datetime | YES | | NULL | |
| isAuto | tinyint(1) | NO | | 0 | |
+------------------+--------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)
1st Child Table,
mysql> desc sattool_desc;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| sattool_desc_id | bigint(20) | NO | PRI | NULL | auto_increment |
| ceinterface | varchar(50) | YES | | NULL | |
| cehostname | varchar(50) | YES | | NULL | |
| vprnno | varchar(50) | YES | | NULL | |
| policyname | varchar(50) | YES | | NULL | |
| cosno | varchar(10) | YES | | NULL | |
| peipaddress | varchar(17) | YES | | NULL | |
| router_type | varchar(5) | YES | | NULL | |
| sattool_testing_id | bigint(20) | YES | | NULL | |
| epipeid | varchar(20) | YES | | NULL | |
| cerouter | varchar(10) | YES | | NULL | |
| service_type | varchar(10) | YES | | NULL | |
| scope_of_manage | varchar(50) | YES | | NULL | |
| service_name | varchar(10) | YES | | NULL | |
| ce_vrf_name | varchar(150) | YES | | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
15 rows in set (0.01 sec)
2nd Child Table (Contain multiple records against one parent table record),
mysql> desc sattool_error_log;
+----------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+-------------------+-----------------------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| service_id | varchar(30) | NO | | NULL | |
| coff_id | varchar(50) | NO | | NULL | |
| error_name | varchar(100) | NO | | NULL | |
| error_desc | text | NO | | NULL | |
| error_occurred | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| error_type | int(11) | YES | | NULL | |
| error_code | varchar(10) | YES | | NULL | |
| sattool_id | bigint(20) | YES | | 0 | |
| module_no | int(11) | YES | | 0 | |
+----------------+--------------+------+-----+-------------------+-----------------------------+
10 rows in set (0.00 sec)
SQL Query :-
SELECT GROUP_CONCAT(sattool_error_log.error_desc) AS err_desc, GROUP_CONCAT(sattool_error_log.error_type) AS err_type, sattool_testing.*, sattool_desc.service_type, sattool_desc.service_name, sattool_desc.scope_of_manage
FROM sattool_testing
LEFT JOIN sattool_desc ON sattool_testing.id = sattool_desc.sattool_testing_id
LEFT JOIN sattool_error_log ON sattool_testing.id = sattool_error_log.sattool_id
WHERE sattool_testing.isAuto = 1 GROUP BY sattool_testing.id ORDER BY sattool_testing.id DESC limit 100
Current Execution Time is (22.76 sec)
Explain Query :-
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | sattool_testing | ALL | NULL | NULL | NULL | NULL | 3578 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | sattool_desc | ALL | NULL | NULL | NULL | NULL | 4009 | |
| 1 | SIMPLE | sattool_error_log | ALL | NULL | NULL | NULL | NULL | 8904 | |
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------------------------------------+
3 rows in set (0.00 sec)
I want to increase speed to above query execution.
Please give me suggestion on that.
Thanks!!!
I have added index to the columns used in the join/where clauses.
Like :-
ALTER TABLE sattool_desc ADD INDEX sattool_testing_id (sattool_testing_id);
ALTER TABLE sattool_error_log ADD INDEX sattool_id (sattool_id);
ALTER TABLE sattool_testing ADD INDEX isAuto (isAuto);
Now the execution time is 100 rows in set (0.00 sec).
Thanks again.
I am trying to solve a probably simple thing, being no expert at all # mysql
I have this first table 'online_players'
mysql> show columns from online_players;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Agent | varchar(255) | YES | | NULL | |
| Name | varchar(255) | YES | | NULL | |
| Alias | varchar(255) | YES | | NULL | |
| Begin_Date | varchar(100) | YES | | NULL | |
| LastBalanceUpdate | varchar(100) | YES | | NULL | |
| Session_minutes | varchar(100) | YES | | NULL | |
| Balance | varchar(100) | YES | | NULL | |
| Product | varchar(100) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.04 sec)
which is droped, recreated and value inserted every 3 minutes, it shows players on a website currently playing...
I then have this table 'players_count' :
mysql> show columns from players_count;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| date_count | varchar(50) | YES | | NULL | |
| players_count | varchar(10) | YES | | NULL | |
| product | varchar(10) | YES | | NULL | |
| avg_balance | varchar(100) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+---------------+--------------+------+-----+---------+----------------+
5 rows in set (0.07 sec)
When the table online_players is updated, the table players_count is updated with a specific insert:
USE livefeed;
INSERT INTO players_count(
date_count,players_count,product, avg_balance)
SELECT
now(),
count(Name) as Players,
Product,
Avg(Balance)
FROM
online_players
GROUP BY Product;
It works ok when at list 1 player is online:
mysql> select * from players_count
-> ;
+---------------------+---------------+---------+-------------+----+
| date_count | players_count | product | avg_balance | id |
+---------------------+---------------+---------+-------------+----+
| 2016-03-28 17:09:02 | 2 | USD | 0.06 | 1 |
| 2016-03-28 17:12:01 | 1 | USD | 0.12 | 2 |
| 2016-03-28 17:15:00 | 1 | USD | 0.12 | 3 |
| 2016-03-28 17:18:00 | 1 | USD | 0.12 | 4 |
| 2016-03-28 17:21:01 | 1 | USD | 0.12 | 5 |
| 2016-03-28 17:24:00 | 1 | USD | 0.12 | 6 |
+---------------------+---------------+---------+-------------+----+
6 rows in set (0.21 sec)
But I would also like to record a row in table players_count when NO player is online even if my query above = 0, for example:
+---------------------+---------------+---------+-------------+----+
| date_count | players_count | product | avg_balance | id |
+---------------------+---------------+---------+-------------+----+
| 2016-03-28 18:01:00 | 0 | USD | 0 | 7 |
| 2016-03-28 18:01:00 | 0 | FUN | 0 | 8 |
I use the table players_count to generate "real time" graphs and need to have 0 values for every specific date.
It might be simple but I have not found any solution online...
How shoud I modify my Insert/query to do this?
Thanks for your help
I have the following existing table in a mysql database:
+---------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------+----------------+
| f1 | int(11) | NO | PRI | NULL | auto_increment |
| field2 | int(11) | NO | MUL | NULL | |
| field3 | int(11) | YES | | NULL | |
| field4 | int(11) | YES | | NULL | |
| field6 | varchar(64) | YES | | NULL | |
| field7 | varchar(16) | YES | | NULL | |
| field8 | varchar(32) | YES | | NULL | |
| field9 | varchar(128) | YES | | NULL | |
| field10 | varchar(128) | YES | | NULL | |
| field11 | varchar(128) | YES | | NULL | |
| field12 | varchar(64) | YES | | NULL | |
| field13 | varchar(32) | YES | | NULL | |
| field14 | varchar(32) | YES | | NULL | |
| field15 | int(11) | YES | MUL | NULL | |
| field16 | date | YES | | NULL | |
| field17 | date | YES | | NULL | |
| field18 | int(11) | YES | MUL | NULL | |
| field19 | varchar(64) | YES | | NULL | |
| field20 | varchar(64) | YES | | NULL | |
| field21 | varchar(16) | YES | | NULL | |
| field22 | varchar(20) | YES | | NULL | |
| field23 | varchar(1000) | YES | | NULL | |
| field24 | int(11) | NO | MUL | NULL | |
| field25 | int(11) | NO | | 0 | |
| field26 | decimal(19,2) | YES | | 0.00 | |
| field27 | decimal(19,2) | YES | | 0.00 | |
| field28 | int(11) | YES | MUL | NULL | |
| field29 | int(11) | YES | MUL | NULL | |
| field30 | varchar(128) | YES | | NULL | |
| field31 | varchar(128) | YES | | NULL | |
| field32 | varchar(16) | YES | | NULL | |
| field33 | int(11) | YES | | NULL | |
| field34 | int(11) | YES | | NULL | |
| field35 | varchar(128) | YES | | NULL | |
| field36 | int(11) | YES | MUL | NULL | |
| field37 | int(11) | YES | | NULL | |
+---------------------+---------------+------+-----+---------+----------------+
I try the following statement to add another row and I'm getting the following error:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
Here are the ways I've tried to insert the row into the table:
insert into table (Field, Type, Null, Key, Default, Extra) VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
insert into table VALUES ('contract_expiration','date','YES','','NULL','');
Both return the same error. There are no triggers on the table, I'm not sure what's going on.
Any suggestions? I'm relatively new to mysql administration, I know a bit but this has me stumped and searches for solutions have turned up nothing.
Any help that could be provided would be MUCH appreciated!
NULL is not a valid field name in:
insert into `table`(Field, Type, Null, Key, Default, Extra)
VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
And Key and default are reserved words. Try this:
insert into `table`(Field, `Type`, `Null`, `Key`, `Default`, Extra)
VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
mysql> desc classroom;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| clId | int(11) | NO | PRI | NULL | auto_increment |
| clFName | varchar(30) | NO | | NULL | |
| clSName | varchar(10) | NO | | NULL | |
| clCapc | int(3) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from classroom;
+------+------------+---------+--------+
| clId | clFName | clSName | clCapc |
+------+------------+---------+--------+
| 1 | Classroom1 | cl1 | 100 |
| 2 | 2 | 2 | 2 |
| 3 | 3f | 3s | 3 |
| 4 | 3f | 3s | 3 |
| 5 | class4 | class4 | 100 |
+------+------------+---------+--------+
5 rows in set (0.00 sec)
I also have same error
mysql> insert into classroom values('Gudadhe', 'Akash', 20);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
This error can be solved by specifying column names before inserting directly as follows
By writing classroom(clFName, clSName, clCapc) we are specifying columns into which we want to insert values
mysql> insert into classroom(clFName, clSName, clCapc) values('Gudadhe', 'Akash', 20);
Query OK, 1 row affected (0.06 sec)
I had a similar case, where I took the table definition from a dev server and the data from a live server, and it turned out that they were actually not quite the same.
The way I found out the difference was (there are probably smarter ways to do it, but this is how I did it):
SHOW CREATE TABLE mytable;
I ran this on all 3 cases (live database, dev database, and new database I created using CREATE TABLE xxx like xxx).
Then I simply compared the 3 and found that the live and dev had a different set of columns, so I simply ran
ALTER TABLE xxx DROP yyy;
until the new table was the same as the table the dump was from; then I could import data.