Overriding an implied database in a MySQL UNION? - mysql

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 ...

Related

How can I tell how many rows or objects are in my table using MySQL Workbench?

How can I tell how many rows or objects are in my table using MySQL Workbench ?
I think I have 9000 items, but since I can only select 1000 as my limit in Workbench. Now I am not sure whether or not my data is there.
SELECT COUNT(*) FROM tablename;
I should note that not every table engine can provide exact results without a WHERE part. InnoDB in particular. But it should be enough to just do something like:
SELECT COUNT(*) FROM tablename WHERE some_column IS NOT NULL;
On a NOT NULL defined column, for example.
This will provide us more detail about the table.
Right click, and click Table Inspector
According to my situation, the fourth line is exactly what I want to know.
Table rows : 34165

MySQL creating view which is union of two tables with one tables values prefixed

I have authentication system which gets permissions from MySQL database.
CREATE TEMPORARY TABLE db.temp (authority VARCHAR(100));
INSERT INTO db.temp (authority) SELECT blog.authors.id FROM db.authors;
UPDATE db.temp SET authority=CONCAT("AUTH_",authority);
SELECT authority FROM db.temp
UNION
SELECT authority FROM db.authorities_real
This script works fine when executed from command line, but in Views, use of temporary tables is not allowed. Is there any way to do what i want without them?
Why not just UNION using the authors table:
SELECT CONCAT('AUTH_',blog.authors.id)
FROM db.authors
UNION
SELECT authority
FROM db.authorities_real
Instead of creating your temp table, you will just use the db.authors table in the UNION. Doing it this way you do not have to use a temp table.
The UNION will remove any duplicates from the query. If you don't mind the duplicates or do not expect any then I would use a UNION ALL
You can combine those statements into a single one:
SELECT concat('AUTH_', blog.authors.id) as authority FROM db.authors
UNION -- all ?
SELECT authority FROM db.authorities_real`

Cannot access table in 2nd database from MySQL query

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;

MySQL shared table or table in each database

I have such a situation, in my system there are many databases and for some queries I need to use the same table "hours", which basically has one field 'h' and stores hours like '00',...,'23'.
My question is about efficiency, is it better to create separate database and store this table there or have this table in each database. My queries will look like:
SELECT ... FROM hours CROSS JOIN some_table ...
Thank you!
Either way, you'd be having to modify all the "non-local" queries to use that table
SELECT ...
FROM sometable
CROSS JOIN dbname.hours
If it's stuck into its own database, then you have to modify ALL queries. If it's in one of the 'real' databases, you only have to modify n-1 queries. Plus having to grant appropriate permissions on that table if you're using multiple different mysql accounts for the different databases.

MySQL Tables and row count

I am trying to get a list of tables and their number of rows. I have been using this query:
SELECT TABLE_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA WHERE TABLE_SCHEMA = 'myDatabase'
I am finding this sometimes returns null. What I would like to do is catch this probably doing something similar to
IFNULL ( TABLE_ROWS, SELECT COUNT(*) FROM ????)
Only I'm not sure what I should enter for ????
how to make it dynamic based on the TABLE_NAME column?.
Edit: Additional information: I found that the 'tables' not displaying are actually views.
Might as well do
SHOW TABLE STATUS FROM YOUR_DATABASE;
It always return a column named as Rows which is number of rows in that table.
From the documentation:
The TABLE_ROWS column is NULL if the table is in the INFORMATION_SCHEMA database.
So, you will propably not need it.