If I have this statement,
SELECT table1.*, table2_1.`values` AS val_1
FROM table1 JOIN table2_1
ON table1.row_names=table2_1.row_names
I would actually like this the result joined back into table1. Any inclusion of a join statement after SELECT and before FROM gives me an error.
Why can I not save the results to a table, and is it possible to save it back to one of the original tables?
Because you do it the wrong way. SELECT statement will not modify you data no matter how much you want it. If you want to modify data, you need to use UPDATE statement.
Related
What I want to achieve (if it is possible from SQL Developer) is that when I execute the script it do the following:
Run a SELECT statement that will return a list of IDs. Approx 270 records.
I need to use each of those IDs individually in another SELECT statement (in the WHERE clause) which will return some records. A few of this could result in over 17,000 records and some can be one.
Then each result from the second SELECT I want it to be exported to an excel or csv file into a folder at my pc.
I have both 'Select' ready but I don't know how to loop over the results of the first to grab each ID and use it in the second one. Also I don't know how to export automatically from the code.
You can use GROUP BY clause.
read more here:
http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html
If the first SELECT returns you IDs only, and that's all you need for your second SELECT, just use IN clause providing your first SELECT query for the second one.
Example:
-- Your second select
SELECT
col1
,col2
,col3
,col4
FROM
second_table
WHERE
some_foreign_id IN (
-- Your first select
SELECT
id
FROM
first_table
WHERE
some_conditions...
)
In my opinion don't use PLSQL for this. In PLSQL the only way you can get an output of this by using a REFCURSOR (unless you dont use UTIL File package to do it). A simple SELECT WITH JOIN condition will suffice your requirement.
Test illustration
SELECT A.* FROM TABLE_A A, TABLE_B
WHERE A.COMMON_COLUMN = B.COMMON_COLUMN;
Hope this helped you in same way
I execute a query in my DB:
SELECT table1.*, tabl2.* FROM table1 JOIN table2 USING(id);
In these 2 tables i have a common column "id". What I have to ask, in order to get the column 'id' once time in the results and not twice?
I thought one solution is to write down in the query which columns I want. But If I want to avoid this (as there are many) ?
Will it work for you to name specific columns you need from both tables? something like:
SELECT table1.id, table2.other_column1, table2.other_column2 FROM table1 JOIN table2 USING(id);
You are selecting all fields from both tables by using (*)
I'm working on a sqlite database and try to make a special request between two tables.
In the first table (table1 for example), i have two columns named "reference" and "ID". I want to search an ID in it, get it value in "reference" and display all informations from the table which have this value as name.
I try to find something on the internet but I didn't find an answer.
This is the request I made:
select * from (select Reference from table1 where Name='Value1')
It only give me the result of
select Reference from table1 where Name='Value1'
EDIT:
I want
select Reference from table1 where Name='Value1' => name of table
select * from name of table => show all elements
I'm new in sqlite but I hope you can help me.
Thank you by advance
Matt
If I understand your question correctly, I don't think there's a way to do it in sql completely (or at least not in a portable way). I'd recommend one of 3 solutions:
Do exactly what you want, but do some processing in Python. That means query your master table, then construct new query based on each of the rows returned.
If you have many tables, possibly changing dynamically - it may be a good idea to rethink your database design. Maybe you can move some of the changing table names into a new column and put your data in one table?
If you have only a few tables available as the Reference and they never change, you could join all the possible tables, like:
SELECT ... FROM table1
LEFT JOIN table2
ON table1.id = table2.id AND table1.Reference = "table2"
LEFT JOIN table3 ...
But you may need to explain it all a bit better...
I have 2 tables, headers with 2 millions rows and files with 30 rows.
I have a query that supposes to get the total number of headers for each directory.
The SQL looks like below:
SELECT files.dir_id, COUNT(*) AS "TOTAL"
FROM headers
LEFT JOIN files ON headers.file_id = files.file_id
GROUP BY files.dir_id
Currently, executing the SQL above is taking 20sec. How can I index it to make it faster?
I have tried CREATE INDEX IF NOT EXISTS HEADERS_FILE ON HEADERS(FILE_ID). This made GROUP BY file_id gets instant response (without left joining files table). However, it doesn't improve the performance for the original query above.
I'm thinking of something like CREATE INDEX INDEX_NAME ON HEADERS, FILES(FILE_ID, DIR_ID) should work. but I find no way in creating such index.
Appreciate for any help. Thanks!
The LEFT join prevents the database from using files as the outer table in the nested loop join.
Try using an inner join, and then adding the missing rows by hand; this might allow better optimization for the two subqueries:
SELECT files.dir_id, COUNT(*) AS "TOTAL"
FROM headers
-- LEFT JOIN expanded by hand for better optimization
INNER JOIN files ON headers.file_id = files.file_id
GROUP BY files.dir_id
UNION ALL
SELECT dir_id, 0
FROM files
WHERE file_id NOT IN (SELECT file_id
FROM headers)
Using the Chinook test database, the following SQL statement works in SQLite:
SELECT * FROM playlist WHERE Name LIKE '%the%'
Yet if I use JOINs:
SELECT * FROM playlist AS pl
JOIN playlisttrack AS plt ON pl.PlaylistId=plt.PlaylistId
JOIN track AS t ON plt.TrackId=t.TrackId WHERE pl.Name LIKE '%the%'
SQLite fails on the WHERE statement, although MySQL works fine:
What makes SQLite fail here?
Are you sure you have playlisttrack and track for your playlist?
Replace JOIN by LEFT JOIN to get all playlist even if they don't have playlisttrack or track
The LIKE seems to work, as you get the same result.
The problem seems to be multiple columns with the name "Name" in the three tables. I would avoid "SELECT *" and select the columns you need instead, using aliases to make clear what is what.