Cannot access table in 2nd database from MySQL query - mysql

I have 2 databases on a server:
database1
database2
I am trying to query a table in each database. However, when I try that, I get the following error:
Unknown table 'database2.client' in field list
Here is the code that I am using in a script running on database 1:
SELECT database2.client.id;
It returns the unknown table error. The odd thing is that if I query for the databases, it shows them both:
SHOW DATABASES
it returns the following:
information_schema
database1
database2
I'm not sure why the SHOW syntax confirms that both databases are there, but I'm not able to select data from the other table.

Try this:
SELECT id FROM database2.client;

I believe the correct syntax would be:
SELECT id FROM database2.client
Where id is the column, database2 is your database, and client is your table.

Can you select it if you first run use database2, and then SELECT client.id?
EDIT:
As many people have pointed out, you need to use the correct SELECT syntax.
USE database2;
SELECT id FROM client;

Related

How to view the list of SQL queries executed in RDS MYSQL

I would like to view the following columns in the query output in RDS MySQL. Could you help me to write sql query against the right system tables to view all the sql queries (from one specific db or all db ) executed by one specific database user or all users ?
The columns that I am trying to fetch are -
database user name
database name
tablename,
sql query id,
sql query text,
query start time
query end time
For example, I executed select count(*) and then trying to see the list of comamnds that I had executed, so, I tried to
query them using the system table "INFORMATION_SCHEMA.PROCESSLIST" but i coudldn't find the same. Please guide/correct me.
I connect the instance using "testuser" credentail and execute the following
create database testdb;
use testdb;
create table testdb.table01 as select * from testdb01.Persons;
select count(*) from testdb.table01;
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE USER='testuser';
228336 testuser 10.xx.xxx.xxx:50881 Sleep 14
228337 testuser 10.xx.xxx.xxx:50882 testdb Query 0 executing SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE USER='testuser'
LIMIT 0, 1000
Thank you,
Kind regards,
sk
The INFORMATION_SCHEMA.PROCESSLIST is a monitor for all the corrently active queries on your DB, but if they are fast to execute you most likely never see them in that table
You could try to use the MySQL integrated logging procedure insted by logging all the queries within the mysql.general_log table and filter the user_host column
Refere to this question Log all queries in mysql
Keep in mind that this table can grow in size very quickly if not frequently cleared, also it can cause some performance issues due to resource consumption

how to query from multiple table of mysql database

I have a login system where i need to query from 2 sql table. The columns name are same in both table e.g - Email. The goal is if the value user entered is not found in first table it will look at the second table.is it possible by any chance?
Yes, it is possible.
SELECT table1.email AS table1_email, table2.email AS table2_email FROM table1, table2 WHERE table1.email='whateveremail#gmail.com' OR table2.email='whateveremail#gmail.com'

How to count no. of existing tables in mysql database

I am sometimes curious just to do a quick command line query to count the number of tables in my database. Is that possible in MySQL? If so, what is the query?
You can try this SQL query:
USE <database_name>;
SELECT COUNT(*) FROM sys.tables;
In the command above, replace the placeholder with the actual database name.

Overriding an implied database in a MySQL UNION?

I swear, this is for educational purposes only...
The MySQL query looks like this:
SELECT *
FROM mytable
where name='value' AND addr='value'
UNION SELECT *
FROM mysql.user;
But the error I get is that there is no mysql.mytable table.
Is there anything I can do in my union or in either of those values to make my query valid? Other than explicitly stating the mytable database name in the code? Let's assume I can't change anything in the query before the 'value'.
It's not very clear what are you trying to select from information_schema.user. information_schema is the standard MySQL database, it's there by default. However there is no user table in this database.
To view a list of your databases, do this in your MySQL console:
show databases;
To view tables in a database:
use information_schema
show tables;
From what you are saying, it looks like you are using information_schema database by default. Also you are trying to select mytable in this database, this table doesn't seem to exist either. Can you run the commands above and show us the output?
Update: If you need to select from two different databases:
select * from mydatabase.mytable union select * from mysql.users
Either remove mydatabase if mytable is in your current database or provide a proper name of your database instead. mydatabase here is just an example.
However, as already been pointed out to you, you should either have identical columns in both tables, or specify identical columns to be selected from each table.
For functional and valid UNION clause, you must have the same columns in both queries. Missing columns in a select, highlight default NULL with aliases:
Select *, NULL as "column1" FROM table1 ...

count of the column names in phpmyadmin (mysql)

this seems stupid, but I really don't know this thing.
Today, I am in need to get number of properties in a mysql table (in phpmyadmin).
Can I get the count of the column names in phpmyadmin (mysql)?
For that You can write sql query in phpmyadmin.
SELECT count(*)
FROM information_schema.columns
WHERE table_name = '<table_name>'