MySQL query on all databases with the same table and column names - mysql

Assume we have the following 2 databases:
DROP DATABASE IF EXISTS `adb`;
CREATE DATABASE `adb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `adb`;
CREATE TABLE IF NOT EXISTS `Login` (
`ID` bigint(20) NOT NULL,
`Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `Login` (`ID`, `Login`) VALUES
(1, '2012-11-09 11:18:29'),
(2, '2012-12-22 21:48:48'),
(3, '2013-01-01 12:39:22');
DROP DATABASE IF EXISTS `bdat`;
CREATE DATABASE `bdat` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `bdat`;
CREATE TABLE IF NOT EXISTS `Login` (
`ID` bigint(20) NOT NULL,
`Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `Login` (`ID`, `Login`) VALUES
(1, '2011-11-09 15:15:15'),
(2, '2012-12-22 13:08:18'),
(3, '2010-02-11 17:00:02');
We also have 2 queries.
Query1 is:
SELECT table_schema AS "Database", round(sum(data_length+index_length)/1024/1024,4) AS "Size (MB)" FROM information_schema.tables GROUP BY table_schema;
Query2 is:
SELECT Max(Login) AS "Last Login" FROM Login
How to combine the two queries together to get the following result ?
Database Size (MB) Last Login
adb 0.0020 2012-12-22 13:08:18
bdat 0.0020 2013-01-01 12:39:22
information_schema 0.0078 NULL
mysql 0.6133 NULL

If you use a Mysql version greater than 5.0 can use FEDERATED TABLES.
For example, in BDAT create a FEDERATED TABLE to ADB, with this code:
CREATE TABLE federated_Login (
`ID` bigint(20) NOT NULL,
`Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://root#remote_host:9306/federated/Login';
And then you can use federated_Login as a local table for BDAT, for more information:
http://dev.mysql.com/doc/refman/5.0/es/federated-use.html

Related

Get data of table with some data from other table

I have a simple table visitor and another table visitor_tokens.
SQL creation script of visitor:
CREATE TABLE `visitor` (
`id` int(7) UNSIGNED NOT NULL,
`phone` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE `visitor`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `phone_index` (`phone`);
ALTER TABLE `visitor`
MODIFY `id` int UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
INSERT INTO `visitor` (`phone`) VALUES
('111111111');
SQL creation script of visitor_tokens:
CREATE TABLE `visitor_tokens` (
`id` int(7) UNSIGNED NOT NULL,
`visitor` int(7) UNSIGNED NOT NULL,
`token` varchar(100) COLLATE utf8mb4_general_ci NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `visitor_tokens`
ADD PRIMARY KEY (`id`);
ALTER TABLE `visitor_tokens`
MODIFY `id` int UNSIGNED NOT NULL AUTO_INCREMENT;
INSERT INTO `visitor_tokens` (`visitor`, `token`) VALUES
(1, 'abc_token'),
(1, 'xyz_token');
I want to get some data of visitor by id:
I want to get the phone number (phone column) and the visitor tokens (visitor_tokens.token). All - according to given id.
My current SQL script is: SELECT visitor.phone, visitor_tokens.token FROM visitor JOIN visitor_tokens ON visitor_tokens.visitor=visitor.id WHERE id=1. This gives me only the phone and the first token: abc. But I also want to get the tokens of the visitor from the second table. To get something like [abc_token, xyz_token]. How can I do it?
So this is my solution (also recommend on reading the main comments):
SELECT visitor.phone, GROUP_CONCAT(visitor_tokens.token) AS tokens FROM visitor LEFT JOIN visitor_tokens ON visitor_tokens.visitor=visitor.id WHERE visitor.id=1 LIMIT 1;
It returns result regardless of missing visitor tokens (LEFT JOIN), and also if there are tokens - it returns the tokens separated by comma in one row.

Issue with Query using Aurora-MySQL works with MySQL

We have a simple query that checks if a record exists in one table that has the id of another. If so it returns the id. This works for small data sets such as the working example below in Aurora.
However, if we have a lot of ids (like 1000) the first query returns 0 rows and the second succeeds in Aurora but works on MySQL.
We have been unable to see any optimizations or other gaps in Aurora MySQL that would explain this behavior. Any advice or guidance is appreciated.
Working example:
CREATE TABLE `test_table_1` (
`id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `test_table_2` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`test_table_1_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
`another_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `test_table_1_id_foreign` (`test_table_1_id`),
KEY `another_id_index` (`another_id`),
CONSTRAINT `test_table_1_id_foreign` FOREIGN KEY (`test_table_1_id`) REFERENCES `test_table_1` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `test_table_1` (`id`)
VALUES
('e54b3097-21ff-4d7d-8bee-5aa62e66d9b6'),
('4fada427-ef5a-455d-ad55-831de6192dc1');
INSERT INTO `test_table_2` (`test_table_1_id`, `another_id`)
VALUES
('e54b3097-21ff-4d7d-8bee-5aa62e66d9b6', 'aeef668e-1365-41e8-a4a7-026d9a021935'),
('4fada427-ef5a-455d-ad55-831de6192dc1', '61e7b307-e356-4537-bcc5-927c3c217992');
SELECT * FROM `test_table_1`
WHERE `id` = 'e54b3097-21ff-4d7d-8bee-5aa62e66d9b6'
AND EXISTS (
SELECT * FROM `test_table_2`
WHERE `test_table_2`.`another_id` = 'aeef668e-1365-41e8-a4a7-026d9a021935'
AND `test_table_2`.`test_table_1_id` = `test_table_1`.`id`);
SELECT * FROM `test_table_1`
WHERE `id` = '4fada427-ef5a-455d-ad55-831de6192dc1'
AND EXISTS (
SELECT * FROM `test_table_2`
WHERE `test_table_2`.`another_id` = '61e7b307-e356-4537-bcc5-927c3c217992'
AND `test_table_2`.`test_table_1_id` = `test_table_1`.`id`);

Not able to store emojis on a utf8mb mysql table

Having this table using mysql 5.7:
CREATE TABLE `emails` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subject` varchar(191) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
When I try to insert some emojis:
INSERT INTO `emails` (`from_address`, `subject`) VALUES (1, 'A😀B C👨🏽‍🎨D')
I receive:
Incorrect string value: '\xF0\x9F\x98\x80B ...' for column 'subject' at row 1
Why? if i'm using utfmb?
Is your connection also utf8mb4? Detailed explanation of this can be found at: https://mathiasbynens.be/notes/mysql-utf8mb4

error while importing .sql to phpMyAdmin db

I want to import an SQL file to my phpMyAdmin database, the file contains tables about a restaurant application and a table about the admin login, but I have 2 unexpected errors, I really don't see what is exactly wrong with that?. here is the error message:
2 errors were found during analysis.
An opening bracket followed by a set of values was expected. (near "CREATE" at position 109)
Unexpected token. (near "CREATE" at position 109)
SQL query:
INSERT INTO `adminlogin` (`id`, `Username`, `Password`, `right`) VALUES (1, 'admin#gmail.com', '123', 1), CREATE TABLE IF NOT EXISTS `tbl_food` ( `id` int(11) NOT NULL AUTO_INCREMENT, `food_type` varchar(50) NOT NULL, `food_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `tbl_food` (
`id` int(11) NOT NULL AUTO_INCREMENT,
' at line 6
and here is the two tables that show errors. Any insight on that? thank you
CREATE TABLE IF NOT EXISTS `adminlogin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Username` varchar(100) NOT NULL,
`Password` varchar(50) NOT NULL,
`right` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `adminlogin`
--
INSERT INTO `adminlogin` (`id`, `Username`, `Password`, `right`) VALUES
(1, 'admin#gmail.com', '123', 1),
-- --------------------------------------------------------
--
-- Table structure for table `tbl_food`
--
CREATE TABLE IF NOT EXISTS `tbl_food` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`food_type` varchar(50) NOT NULL,
`food_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
You have a comma at the end of INSERT statement, instead of semicolon.

SELECT LAST_INSERT_ID() Not working with BIGINT on MySQL 5.6.11

I have a BIGINT field as an auto increment and primary key on a MySQL Innodb table, running MySQL Community Server 5.6.11.
After calling a basic INSERT statement, and then calling SELECT LAST_INSERT_ID(), I'm always returned 0, even though the INSERT statement was sucessful.
Any ideas why this might be happening.
UPDATE: Here is my table definition
CREATE TABLE `Booking` (
`BookingId` bigint(20) NOT NULL AUTO_INCREMENT,
`HotelId` int(11) NOT NULL,
`AgentId` int(11) NOT NULL DEFAULT '0',
`BookedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LastChangedBy` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`BookingId`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is my INSERT statement
INSERT INTO Booking
(
HotelId,
AgentId,
BookedOn,
LastChangedBy
)
VALUES
(
iHotelId,
iAgentId,
NOW(),
0
);
SELECT LAST_INSERT_ID() AS BookingId;