Mysql Query to select all the tables with same column name - mysql

To be specific I am using MySql.I have many tables in my database, like employee, student, employer etc etc. There are more than 20 such tables. And in each table, I have a column called 'username' and 'age'. Now I want a query that will give me the all the ages when i give a particular username='alex'.

You probably should have a single table (say, Users) which has a column indicating what type of person that user is (employee, student, employer, etc.). However, if you must query across multiple tables of this sort, use UNION:
SELECT age FROM employee WHERE username = 'alex' UNION ALL
SELECT age FROM student WHERE username = 'alex' UNION ALL
SELECT age FROM employer WHERE username = 'alex' -- etc.

It looks like poor design of the database and if you can change the database structure, that's probably the way to go. Otherwise, you could try this:
(SELECT username, age
FROM table1)
UNION
(SELECT username, age
FROM table2)
UNION
(SELECT username, age
FROM table3)
...

Query to get and tablenames having a particular column
select table_name from information_schema.columns
where table_name in (
select table_name from information_schema.tables
where table_schema='databaseName'
) and
column_name='username';
from the above query you will be all tablenames which have the field username then i PHP you can build query to get values from all the tables;

Related

Convert Postgres query with distinct on to MySQL query

I have a table users:
Where id is a primary key.
I want to select all columns, but all usernames should be unique. I don't care which ids will be in an expected result, but anyway I need them. For that I use the following query in Posgres 10:
select distinct on (username) * from users;
That gives me the result I want:
How can I achieve the same, but using MySQL query?
Your query doesn't make sense in Postgres because it lacks an order by. For this query:
select distinct on (username) u.*
from users u
order by username, id desc;
You can write this as:
select u.*
from users u
where u.id = (select max(u2.id) from users u2 where u2.username = u.username);
Assuming id is unique, this will return one row per username.
I believe this is the conversion of that
SELECT id, username FROM users group by id
You can find more info on this link:Converting SELECT DISTINCT ON queries from Postgresql to MySQL
Extra Note: You can use this http://www.sqlfiddle.com/#!9/0bd1a2/1 to test your SQL which maybe helpful for you in converting Postgres to SQL

How to Select two tables and Concatenate them

I have two tables Staffs and Customers I want to select the records of each independent of each other but in one query in a such a way that neither of them affects each others' records. for example
Selecting the customers,
Selecting staffs records
,
What I want
As long JOINING and UNION is not for this purpose
You can use Join
Select * from Staffs inner join Customers;
If you just want to get them together into one dataset, you don't need a concatenation. You just need a UNION.
I don't know the exact name of your tables or which RDBMS you are using, but you might try something like:
SELECT *
FROM Staffs
UNION ALL
SELECT *
FROM CUSTOMERS;
If you have the same column names in each, you could do:
SELECT ColumnA, ColumnB, ColumnC AS "Staffs_ColumnC"
FROM Staffs
UNION ALL
SELECT ColumnA, ColumnB AS "Customers_ColumnB", ColumnC
FROM Customers;
It sounds like you want to union them together:
select id, name, lastname, 'staff' as which
from staffs
union all
select id, name, lastname, 'customer'
from customers;

Multiple select statements for mysql search query

can i use select statements for different tables in a single query
SELECT *
FROM users
WHERE first_name LIKE "%'.$search_string.'%"
OR last_name LIKE "%'.$search_string.'%"
thats from the table ' users' , i want to do the same search on another table called sellers. sellers have the same fields first_name, last_name
Yes, you can
Select column1,column2,column3,column4
From table1,table 2
Where Conditions
If you want to do join between tables, check if theirs PK and FK are equals
Eg:
Select table1Id,table2Id,column3,column4
From table1 t1,table2 t2
Where t1=t2
You can search like this
select * from shirts where find_in_set('1','2,3,4,5,1')

can a table be selected in an sql query to group the result

hello i just wanted to know is it possible to select the table name in an sql query as part of your result
so for example when using UNION to join 2 tables can you specify where each result is coming from because so far i have had to add an extra column called type to each table to specify and it just seems there could be a better way
SELECT id, name, type
FROM table1
UNION ALL
SELECT id, name, type
FROM table2
LIMIT 20`
result =
id, name, type
id, name, type
but i want to still have the type/table name without selecting it or even having it in my table
pleas let me know if this is possible or if this is the only way to do it ether way thanks in advance
Try this:
SELECT id, name, type, 'table1' AS FromWhichTable
FROM table1
UNION ALL
SELECT id, name, type, 'table2'
FROM table2
LIMIT 20

Create table as ( using a column and table name)

I need to build a table from several tables in MySql and I want it has two columns like:
-------------------
name | table_name |
-------------------
I'm doing this:
Create table teams as
(
Select name from table1
union
Select name from table2
union
Select name from table3);
How could I include each table name as a second column??
Thank you!
Include a quoted string literal. Be sure to also give it a column alias, which will be used as the column name in the resultant table.
CREATE TABLE teams AS
/* Quoted string literal with column alias */
(SELECT name, 'table1' AS `tablename` FROM table1)
UNION
(SELECT name, 'table2' AS `tablename` FROM table2)
UNION
(SELECT name, 'table3' AS `tablename` FROM table3);
Note that since you are in effect adding a second value to each row which differentiates it from potentially similar rows in the other tables, the UNION is now the eqivalent of a UNION ALL, and duplicate rows won't be de-duped as the plan UNION would have. Just beware, that the results may differ from what your original UNION produced.