I'm using MySQL and I selected three tables in one statement like that:
SELECT * FROM tb_i, tb_s, tb_t
But I need case control by their table names. If a row from tb_s it returns tb_s on additional column table_name.
How can I get table name from this statement?
The way you add up your tables now is a JOIN - meaning that every row is a Cartesian product of three of them - so each row is returned from all the three tables.
If you would like to concatenate the data from the three tables you should use UNION ALL which assumes that the columns are of the same structure. Then you would able to mark the origin table, with an addition constant field.
For instance in your case:
SELECT tb_i.*,'tb_i' as source
FROM tb_i
UNION ALL
SELECT tb_s.*,'tb_s'
FROM tb_s
UNION ALL
SELECT tb_t.*,'tb_t'
FROM tb_t
where the source column is a constant string per each table.
Related
Select * from hospital,favorite
Where hospital.h_id=favorite.h_id AND favorite.u_id=$uid;
Is this query valid?
I mean, while joining 2 tables can i select all records from both tables by using *?
Yep, by the SQL specification * means "all columns".
The result of your SQL will have all columns of both tables. In this result some columns may have the same name (if they exist on both tables) and that's OK.
If you want to differentiate columns with the same name, you can assign an alias to each specific column. However, to do this you won't be using * anymore but the full list of columns.
My scenario is as follows( in MySQL)
I have a table say table 1, which has 2 columns:
userID, column_acess
Table 2 which has a list of columns say col1,col2,col3, etc.
Now What I would like to do is use pymySQL to query table 1 for the columns a particular userID is allowed to acesss, by inspecting the column, acess field ( which will contain a comma seperated list of columns in tabl2), and use that result in another sql query ( which works on table2) to actually get the data from the respective columns a user is allowed to acess.
So essentially I would like something like:
Select (Select column_acess from tabl1 where user_ID='123') from table2
So inner query should return the list of columns say col1, col2, which would be used to select the columns in the outer query in table2
How do I do that in mySQL?
I strongly encourage you too read this post. You should either first store columns in variable or use dynamic sql query. Use SELECT result as COLUMN name in other SELECT
BTW your schema is not even in 1 NF since you don’t have atomic values in table 1. You should avoid that.
MySQL supports the granting of column-level privileges to users, using the standard grant statement.
I would suggest that you start with the documentation on this subject.
An alternative to using grant for columns is to create views for different user types. This is, in fact, the more general solution, because the views can filter rows as well as columns. The idea is that the underlying tables are not directly accessible. The views are, so all access needs to go through the views.
I want to get all data from two table on different Databases.I've tried this and only get data from one table.
Select b1.branch.*, db2.branch.* from db1.branch,db2.branch
Is there any way to get that?
Assuming that your branch table have the same struct in both database then you could use an union eg:
Select db1.branch.*
from db1.branch
union all
select db2.branch.*
from db2.branch
for obtain both result sets
I am selecting data from 3 tables [2 tables with a relational table in between them] The below image is a screenshot of the result
Is there a way in which i can apply DISTINCT on certain columns such that i can get the results as below? NB// i have edited the below image
Below is my sql code
No. You can't apply distinct on single column unless, you are pulling just one column from table.
Distinct applies on complete row. It will get you unique rows in table
As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.