It is a bad habbit to use select "*" when writing SQL with JDBC.
But what if there are many columns in the table(s), how can I easily pickup some wanted columns in the SQL rather than using *?
For example,suppose I have table1 and table2,which have 10 columns each. I need most of their columns, but I do not want to use SELECT * FROM table1 inner join table2 on table1.id=table2.id since this is a bad practice. Actually I only need SELECT column1,column4,column5(most of them)...... from table1 inner join table2 on table1.id=table2.id.
So, do I have a way that replace * into all columns and then I can just COPY this text and remove some columns which I don't need and remain what I want rather than type each column name?
If you're using SQL Server, an easy trick is to open a database in Management Studio, right-click on a table and select "Select top 1000 Rows" from the context menu. You'll get all the fields listed one by one. You can copy and paste the text wherever you want.
I don't know which IDE you're using let alone which DBMS, but there's probably a trick like this one in it.
Try using the query below which is use to get all the columns of a specific table. Hope it helps you.
SELECT name FROM sys.all_columns WHERE object_id = OBJECT_ID('MyTableHere')
In MySQL, try the code below or you can refer in this Documentation:
SHOW COLUMNS FROM MyTableNameHere;
Related
I have the following database structure:
And the data stored inside is as follows:
However, I like to select data from this database in the following format:
How can I use one single SELECT statement to achieve this? If using one single SELECT statement cannot, how to achieve this by whatever sql code? Can someone give me the implementation?
Can I use one single SELECT statement to achieve this?
Yes, you can. You will have to play a little with JOIN and GROUP BY statement, but it can be achieved.
EDIT:
Let's try multiple JOIN statements:
Select sum.name, x0.y_value, x10.y_value, x20.y_value
from test_summary as sum
join test_details as x0 on sum.id=x0.id and x0.x_value=0
join test_details as x10 on sum.id=x10.id and x10.x_value=10
join test_details as x20 on sum.id=x20.id and x20.x_value=20
Based on answers to multiple joins and join with where clause
The things you like called pivot table, and this is out of scope of relation data base system like mySQL. So there many special platforms for OLAP. But if you do not need any comprehensive OLAP things, but produce some pivot tables using mySQL and PHP you do not need any special sql, but nedd to program such presentation by php, may be using some commercial frameworks like this or some opensource code
I'm trying to get zip codes from zip_id's which are internally stored in companies service table below screens will give you clear idea
I have wrote this query
companies service table
Please suggest me your valuable views . Thanks in advance.
As already mentioned your database scheme is not very well designed, it violates even 1st normal form. You'd need another table where you'd store serv_area_id and zip_code (with possibly multiple rows for a signle serv_area_id) and search within this table and eventually join your original table.
Nevertheless, in order to get the result you describe you cannot use the IN operator as it operates on a value and multiple values in a form of table (either explicit via nested SELECT or enumeration literal (val1, ..., valN)). I would try some string matching as illustrated below. However, consider it rather an ugly hack than correct solution(!)
SELECT zip FROM cities_extended WHERE (
SELECT GROUP_CONCAT(',', serv_are_zipcodes)
FROM company_service_areas WHERE ...
) LIKE concat('%(', id, ')%')
I'm trying to join two tables in peewee with a mysql database. This is pretty easy doing something like this:
s = Table1.select(Table1, Table2).join(
Table2).naive().where(Table1.Title == "whatever")
Unfortunately, I have called a column in Table1 and Table2 the same thing, "URL". Then when I select s.URL it gives me the URL from Table2, which I don't want, I want the one from Table1. Is there some way to either not join the Table2.URL column or to name it something different? This question seems to be addressing a similar problem in regular SQL (not peewee), is there a way to do something similar in peewee?
In other words, I think I'm looking for either a "JOIN AS" method or a "DON'T JOIN THIS COLUMN" method in peewee.
Thanks a lot,
Alex
I haven't used peewee, but the docs suggest that any table, expression, or column has a .alias() method. But that means you'd have to alias the URL column individually:
Table1.select(Table1, Table2.URL.alias('t2_url')).join(Table2)...
Even in hand-crafted SQL, you can't SELECT Table2.* but simultaneously give an alias for one of the columns of Table2.
Hi
I want to use an update inside of select statement e.g:
Select * from admin where id=1 join update data set col1='Mydata'
the table names and format is not important I want to know it's possible or not? If yes, How?
An update inside af a select statement is not possible.
But...
you can use the Update-Statement with specific conditions so the result is the one you need.
Here's a rough example based on your statements:
update data, admin
set data.col1 = 'Mydata'
where data.pkey = admin.pkey
and admin.id = 1;
There should be a link between the two tables which I assumed as "pkey". To be more precise one needs to have a look at the table definitions.
No it is not possible. you must run them separately
You may be able to use JOIN and a WHERE clause in your UPDATE statement. But, you would need to have some way of joining the two tables.
UPDATE admin,data SET data.col1='MyData' WHERE admin.id=1 AND admin.id = data.admin_id;
Or something along those lines. Not sure what your tables look like. Read about the UPDATE statement in the reference manual for your database.
Using Access 2003
I want to get a table value from the two databases
Database – 1
Emp_Table
Database – 2
Customer_Table
Select * from Database-1.Emp_Table, Database-2.Customer_Table
The above query is showing error in the Access. I tried a Join query also, it showing error.
Can any one to solve this problem?
Need Query Help.
Try using square brackets -
SELECT * FROM [Database-1].[Emp_Table], [Database-2].[Customer_Table]
Or, try this.
First, assuming the query is running in Database 1, you will need to create a "linked table" to link to Database 2's table in Database 1.
Once you do that, you can write it simply as:
Select * from Emp_Table, Customer_Table
Since you are "in" database 1, you won't have to qualify Emp_Table, and since you have Database 2's Customer_Table linked in, you won't have to qualify it either.
I don't have Access 2003, but in Access 2007 you can do this:
Click on the "External Data" tab.
Click on the "Access" icon.
Choose the location of your second Access database.
Select "Link to a data source by creating a linked table".
This should add the tables in your second database linked in your original one. You can then write queries to query data from either one or both like you normally would. I'm sure the same functionality is available in Access 2003, just a slighty different visual route to achieve the same thing.
Is there some relationship between the tables, or do you want just a dump of the whole table? Also, post the join you tried and the error you got, it would help in the troubleshooting...
If you want all records from both tables, you would need to use a UNION query like this:
Select * from Database-1.Emp_Table;
UNION Select * from Database-2.Customer_Table;
This assumes that there are the same number of columns in both tables. If not change the * to the specific columns you want to list from each table.