I have a MySQL table that is very simple, containing four fields:
id (autonumbered)
controllerType
onInstructions
offInstructions
I loaded the table with data from a CSV file. The file looks like this:
10,GK 212-02,,
11,GK 212-02 ex,,
12,GK 212-04,,
13,GK 212-06,,
14,GK 212-06 x 2,,
And the query I run looks like this:
load data local infile '/home/garfonzo/controllers.csv' into table systems_controller_type fields terminated by ',' lines terminated by '\n' (id, controllerType, onInstructions, offInstructions);
It loads the data into the table with no warnings:
Query OK, 42 rows affected (0.00 sec)
Records: 42 Deleted: 0 Skipped: 0 Warnings: 0
Then, when I run a select * from systems_controller_type I get this output:
+----+-------------------------------+----------------+-----------------+
| id | controllerType | onInstructions | offInstructions |
+----+-------------------------------+----------------+-----------------+
| | |
| Plus | |
|ar | |
|ar exterior | |
|ird | |
| | |
| | |
| | |
| | |
| | |
| ex | |
| | |
| | |
| x 2 | |
| | |
|06 | |
| | |
| 06 | |
| | |
| | |
| | |
|el | |
|troller | |
| | |
|E-12 | |
|EC-9 | |
| | |
| | |
| | |
|nder (x2) | |
|- 9 station | |
| | |
|rollers | |
|Modular | |
| | |
| | |
|om Command (I think) | |
|lar | |
|mmand | |
| 04 04 04 | |
| | |
| | |
+----+-------------------------------+----------------+-----------------+
If I do a select for one record, I get this:
select * from systems_controller_type where id=3;
+----+----------------+----------------+-----------------+
| id | controllerType | onInstructions | offInstructions |
+----+----------------+----------------+-----------------+
|ar | |
+----+----------------+----------------+-----------------+
1 row in set (0.00 sec)
Any idea as to what is going on?? Did I do something wrong at the import stage?
try to use the following query... (without updating auto increment column - hope you would have set auto increment value while creating the table) ..
load data local infile '/home/garfonzo/controllers.csv' into table systems_controller_type fields terminated by ',' lines terminated by '\n' (#dummy, controllerType, onInstructions, offInstructions);
Related
I have table having few number of columns wherein one column is for Execution number (exe_number) which stores the data in varchar(100).
select*from exedetails;
+------------+------------+------------+-------------+
| exe_number | pass_count | fail_count | error_count |
+------------+------------+------------+-------------+
| 02Aug_E1 | 98 | 9 | 0 |
| 31Jul_E1 | 94 | 8 | 1 |
| 30Jul_E2 | 76 | 9 | 3 |
| 01Aug_E2 | 98 | 7 | 0 |
| 02Aug_E2 | 76 | 8 | 2 |
| 30Jul_E1 | 98 | 12 | 9 |
| 31Jul_E2 | 91 | 6 | 1 |
| 01Aug_E1 | 67 | 14 | 2 |
+------------+------------+------------+-------------+
8 rows in set (0.00 sec)
describe exedetails;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| exe_number | varchar(100) | YES | | NULL | |
| pass_count | int | YES | | NULL | |
| fail_count | int | YES | | NULL | |
| error_count | int | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Here 30/31/01/02 are a date, Aug/Jul is a month and E1/E2 is Execution iteration (This is for my simplicity).
Lets consider present date is
'02 August 2022'
So I want to write a query in such a way that all records starting with
02Aug_
should be at the top of table.
Means It should get arranged according to date so that present day record should be at the top.
Query I wrote for this is not giving desired result:
select exe_number from exedetails order by exe_number desc;
+------------+
| exe_number |
+------------+
| 31Jul_E2 |
| 31Jul_E1 |
| 30Jul_E2 |
| 30Jul_E1 |
| 02Aug_E2 |
| 02Aug_E1 |
| 01Aug_E2 |
| 01Aug_E1 |
+------------+
8 rows in set (0.00 sec)
select exe_number from exedetails order by exe_number asc;
+------------+
| exe_number |
+------------+
| 01Aug_E1 |
| 01Aug_E2 |
| 02Aug_E1 |
| 02Aug_E2 |
| 30Jul_E1 |
| 30Jul_E2 |
| 31Jul_E1 |
| 31Jul_E2 |
+------------+
8 rows in set (0.00 sec)
Expected Result :
+------------+
| exe_number |
+------------+
| 02Aug_E1 |
| 02Aug_E2 |
| 01Aug_E1 |
| 01Aug_E2 |
| 31Jul_E1 |
| 31Jul_E2 |
| 30Jul_E1 |
| 30Jul_E2 |
+------------+
Is there any way in MySQL/PostgreSQL by which I can get my desired solution?
Just convert the "date part" to a proper date value, then you can sort by it:
select *
from exedetails
order by to_date(left(exe_number, 5), 'ddmon') desc,
right(exe_number, 2)
Note that to_date() working with a month name depends on your locale settings and the values in the column.
Online example for Postgres
Do you know if there is any manner to extract maximum number into a string in MySql ?
My table looks like this:
+-----------------------------------------------------------+
| Freq |
+-----------------------------------------------------------+
| 40.56 |
| 99.51 |
| 99.87 |
| T=0.00, TCCA=0.00, TTCC=0.03, TTCG=0.00, TTCT=55.0 |
| C=97.70, T=0.05 |
| 44.61 |
| 57.45 |
| 38.18 |
| 38.43 |
| 39.36 |
| 38.65 |
| 39.30 |
| C=57.53, T=0.17 |
| TAAAAAAAAAAA=0.51, TAAAAAAAAAA=68.03, TAAAAAAAAAAAG=31.42 |
+-----------------------------------------------------------+
Desired output:
55.0
97.70
I've got a file that I've succesfully updated to a table. Now, I'm required to do it again in a way that the data is not being repeated.
my target table:
mysql> show columns from MARCAS;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| ID_MARCA | int(11) | NO | PRI | NULL | auto_increment |
| MARCA | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
So what I did was to create a temporary table and loaded data from the file into it.
code used:
CREATE TEMPORARY TABLE tempe LIKE marcas;
LOAD DATA LOCAL INFILE myfile.txt
INTO TABLE MARCAS
FIELDS TERMINATED BY '#' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID_MARCA, MARCA, #ignore1, #ignore2, #ignore3);
All fine here. The problem starts when I am trying to be smart and use 'ON DUPLICATE KEY'(like it was sugested here) to insert data from temporary table into table MARCAS as so data does not repeat itself. The code I used was:
INSERT into MARCAS select * from TEMPE on duplicate key update MARCA=values(MARCA);
The result:
mysql> select * from MARCAS;
+----------+---------+
| ID_MARCA | MARCA |
+----------+---------+
| 1 | PICASSA |
| 2 | PICASSA |
| 3 | C4 |
| 4 | C4 |
| 5 | C3 |
| 6 | C3 |
| 7 | C2 |
| 8 | C2 |
| 9 | MONDEO |
| 10 | MONDEO |
| 11 | S-MAX |
| 12 | S-MAX |
| 13 | CIVIC |
| 14 | CIVIC |
| 15 | ACCORD |
| 16 | ACCORD |
| 17 | CLS |
| 18 | 900 |
| 19 | LEON |
| 20 | LEON |
| 21 | IBIZA |
| 22 | IBIZA |
| 23 | 307 |
| 24 | 307 |
| 25 | 308 |
| 26 | 308 |
| 27 | 407 |
| 28 | 407 |
| 29 | 408 |
| 30 | 408 |
| 31 | MEGANE |
| 32 | MEGANE |
| 33 | PASSAT |
| 34 | PASSAT |
| 35 | GOLF |
| 36 | GOLF |
| 37 | TOUAREG |
+----------+---------+
Not impressed with myself. What am I doing wrong?
It's caused by the fact that you don't have a UNIQUE index on your MARCA column.
alter table MARCAS add unique(MARCA);
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL
performs an UPDATE of the old row.
source: http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
Please also note that it's also possible to do INSERT IGNORE or A REPLACE when you do a load data.
May you get some idea from this
INSERT INTO MARCAS(field1)
WHERE NOT EXISTS (
SELECT field1 FROM MARCAS WHERE name = 'field1'
) LIMIT 1;
I have apache Hive - 0.12.0 and hadoop - 1.2.1 version on my machine.
and used mysql for hive metastore.
Now when i fire show tables for metastore test, it only shows 20 rows/tables.
// My Environment
mysql> use metastore_db
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_metastore_db |
+---------------------------+
| BUCKETING_COLS |
| CDS |
| COLUMNS_V2 |
| DATABASE_PARAMS |
| DBS |
| PARTITION_KEYS |
| SDS |
| SD_PARAMS |
| SEQUENCE_TABLE |
| SERDES |
| SERDE_PARAMS |
| SKEWED_COL_NAMES |
| SKEWED_COL_VALUE_LOC_MAP |
| SKEWED_STRING_LIST |
| SKEWED_STRING_LIST_VALUES |
| SKEWED_VALUES |
| SORT_COLS |
| TABLE_PARAMS |
| TBLS |
| VERSION |
+---------------------------+
20 rows in set (0.06 sec)
Why am not able to see all the tables like below one. Do i need to use any other hive version for this ? Please suggest.
// Need to see all these tables
mysql> show tables;
+ --------------------------- +
| Tables_in_hive_demo |
+ --------------------------- +
| BUCKETING_COLS |
| CDS |
| COLUMNS_V2 |
| DATABASE_PARAMS |
| DBS |
| DB_PRIVS |
| GLOBAL_PRIVS |
| IDXS |
| INDEX_PARAMS |
| NUCLEUS_TABLES |
| PARTITIONS |
| PARTITION_EVENTS |
| PARTITION_KEYS |
| PARTITION_KEY_VALS |
| PARTITION_PARAMS |
| PART_COL_PRIVS |
| PART_COL_STATS |
| PART_PRIVS |
| ROLES |
| ROLE_MAP |
| SDS |
| SD_PARAMS |
| SEQUENCE_TABLE |
| SERDES |
| SERDE_PARAMS |
| SKEWED_COL_NAMES |
| SKEWED_COL_VALUE_LOC_MAP |
| SKEWED_STRING_LIST |
| SKEWED_STRING_LIST_VALUES |
| SKEWED_VALUES |
| SORT_COLS |
| TABLE_PARAMS |
| TAB_COL_STATS |
| TBLS |
| TBL_COL_PRIVS |
| TBL_PRIVS |
| TYPES |
| TYPE_FIELDS |
| VERSION |
+ --------------------------- +
39 rowsinset (0.00 sec)
Try to upgrade the hive schema by the available sql script (mysql in your case).
mysql> use metastore_db
Database changed
mysql> SOURCE $HIVE_HOME/scripts/metastore/upgrade/mysql/hive-schema-0.12.0.mysql.sql;
mysql> CREATE USER 'dbuser'#'%' IDENTIFIED BY 'dbpassword';
mysql> GRANT all on *.* to 'dbuser'#localhost identified by 'dbpassword';
mysql> flush privileges;
Now requery your tables. It should show all the tables now.
mysql> show tables;
how to alter a mysql table to add a column which contains a common value for all rows by checking another columns repeating value.
+----+----------+--------------+
| id | inode | name |
+----+----------+--------------+
| 1 | 12059010 | IwnsuAaJUFaa |
| 2 | 12059015 | IwnsuAaJUFab |
| 3 | 12059016 | IwnsuAaJUFac |
| 4 | 12059017 | IwnsuAaJUFad |
| 5 | 12059018 | IwnsuAaJUFae |
| 6 | 12059019 | IwnsuAaJUFaf |
| 7 | 12059020 | IwnsuAaJUFag |
| 8 | 12059021 | IwnsuAaJUFah |
| 9 | 12059022 | IwnsuAaJUFai |
| 10 | 12059023 | IwnsuAaJUFaj |
| 11 | 12059013 | iPhZIWtZdSaa |
| 12 | 12059015 | iPhZIWtZdSab |
| 13 | 12059016 | iPhZIWtZdSac |
| 14 | 12059017 | iPhZIWtZdSad |
| 15 | 12059018 | iPhZIWtZdSae |
| 16 | 12059019 | iPhZIWtZdSaf |
| 17 | 12059020 | iPhZIWtZdSag |
| 18 | 12059021 | iPhZIWtZdSah |
| 19 | 12059022 | iPhZIWtZdSai |
| 20 | 12059023 | iPhZIWtZdSaj |
+----+----------+--------------+
i need a fourth column having a common number for all rows group by same "inode" number.
Add the column using something like
ALTER TABLE [tablename] ADD [columname] [columntype]
Change the content of that column using UPDATE.