ALTER TABLE ORDER BY str_to_date and time_to_sec - mysql

I am trying to re-order a table after importing a file but am getting a mysql syntax error on this query:
ALTER TABLE tablename ORDER BY str_to_date(date, '%m/%d/%Y'), time_to_sec(time) ASC
Can anyone see anything wrong with this? this is the error I am getting:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(date, '%m/%d/%Y'), time_to_sec(time) ASC' at line 1

According to mysql manual, ALTER TABLE ORDER BY expects a col_name, but you are trying to order it using a function.
I suggest create 2 new columns, populate them with function results, and then order by them.

You might have to use a temp table for this since you're ordering by a function.
CREATE TABLE temp_tablename AS SELECT * FROM tablename;
TRUNCATE tablename;
INSERT INTO tablename SELECT * FROM temp_tablename;
DROP temp_tablename;
You could make the first statement a CREATE TEMPORARY TABLE, but if you lose your session you lose your data.

Did you try
ALTER TABLE tablename ORDER BY date, time ASC
I mention this because it might give you the order you need.
Otherwise you'll need to either do as German Rumm suggested, add columns with the correct datatype, or do your ordering when you do your SQL.

I think I resolved this issue. I was storing imported data in a temp table before moving it onto it's permanent table and needed to sort the data in the temp table first before inserting into the new table. so what I do instead is insert into the new table via a select statement which has the order by statement.

You are using an older version of mysql, STR_TO_DATE is only availabe from version 4.1.1 of MySQL.
Update MySQL version.

Related

How do I use an IF statement with an ALTER statement in MySQL?

I'm looking to add a column only if it does not already exist before. The motivation for this is that we can upgrade any version of a production instance to the latest version.
This is what I'm trying, but I keep getting a syntax error near the IF statement:
use database_name
SELECT #rowcount:=COUNT(column_name)
FROM information_schema.columns
WHERE table_name = 'table_name'
AND column_name = 'column_2';
IF #rowcount < 1 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
commit;
What am I doing wrong?
You asked:
How do I use an IF statement with an ALTER statement in MySQL?
You Can't Do Thatâ„¢. MySQL's DML and DDL aren't as well integrated as other makes and models of table server.
You can't put DML inside stuff like IF statements or transactions. If you need to do that sort of operation you'll have to use an application programming language to issue simpler SQL.
I think you should change
IF #rowcount < 0 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
and check if table exist.
As #Ollie Jones mentions and from looking at Using an IF Statement in a MySQL SELECT query, it looks like it's not possible to use if statements outside of sprocs or functions.
I followed the steps on http://www.cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm:
Create the sproc (see link)
Call the sproc when wanting to add a new column to a table
Alternatively, as #Ollie Jones mentions, you can do it at an application level, but I'm using .sql files in my case. It also saves on making multiple calls to the DB vs. having it done at the server level.

Making a backup copy of SQL table throws me this error: "#1064 - You have an error in your SQL syntax; "

I'm trying to make a backup of my table in MySql but I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)

How to identify the table name from last insert or update in sql server and mysql

In sql server and mysql
I want a query to identify the tables name which are affected using INSERT or UPDATE
Additional info:
1. I have more than two tables and all tables may not have indexes.
2. If a stored procedure execute I don't know what are the tables inserted or updated. But here I want to know.
Thanks.
You can do this in SQL server
SELECT TOP 1 *
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'AdventureWorks')
AND OBJECT_ID in (select OBJECT_ID(name) from sys.tables)
ORDER BY last_user_update DESC
not sure about MYSQL
taken from here and here

Use of SELECT INTO

Is it necessary to define the new table definition before using SELECT INTO query in MYSQL.
I am getting problem to execute the query when I writ e like:
SELECT *
INTO newtable
FROM oldtable
WHERE 1=0;
the error showing is:
Undeclared variabie: newtable
if you have newtable
try :
INSERT INTO newtable SELECT ...
if you don't have newtable
try :
CREATE TABLE newtable AS SELECT ...
The MySQL manual search engine is terrible but googling for something like mysql 5.5 select into will normally take you to the right page:
MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL
extension. Instead, MySQL Server supports the INSERT INTO ... SELECT
standard SQL syntax, which is basically the same thing.
If you read the documentation here it says:
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables
So, yes, you need to create the new table first.
You can use CREATE TABLE new LIKE old to create a new, empty table, which is a copy of the original table structure.

Existence confirmation method of the table of MySQL

I want to confirm whether there is a certain table.
When create a table, there is an SQL sentence such as DROP TABLE IF EXISTS xxx_tb.
Will there be the method that can identify the existence of the table by SQL likewise?
Use INFORMATION_SCHEMA:
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'MyTable';
Should be portable across most databases.
You want the SHOW TABLES command of MySQL:
SHOW TABLES LIKE 'xxx_tb';
Or indeed, you can just do a query like
SELECT COUNT(*) FROM tbl WHERE 1=0
Which will give an error (see documentation for exact error code, or try it) if the table doesn't exist, but succeed with no results if it does.