How to insert values into temp table while executing query - mysql

I am using laravel framework for developing APIS , i have a challenging situation while executing my current query at that time i have to create temporary table with some columns and i have to insert some data based on current running query column values,can anyone please give me some idea how to acheive this one?
$q = user::query();
//some joins and where conditions stuff is there here
$q->addselect(“col1 as UserName”)
if($data[“name”]==“test”){
DB::statement('CREATE TEMPORARY TABLE temp SELECT ...i want to insert UserName value here in my temp table column called tempCol');
}
$q->paginate();

Related

I have issue with CREATE TABLE and then INSERT you table

I have issue width CREATE TABLE and then INSERT you table.
This is what I want to do. CREATE TABLE LIKE and then INSERT SELECT * FROM.
I be creating multiple tables at the same time. I will be using a table like sample table to create the tables. Then editing the new tables in the future.
This Sample table.
id
old_data
new_data
1
pool_00
pool_01
2
pool_00
pool_02
This is MySQL code I’m having issues with.
CREATE TABLE `new_data` LIKE `old_data`;
INSERT `new_data` SELECT * FROM `old_data`;
This is the error I get.
Error
SQL query:
CREATE TABLE new_data LIKE old_data
MySQL said: Documentation
#1146 - Table 'oerpn_survivor_us.old_data' doesn't exist.
I run this MySQL code and it works.
CREATE TABLE pool_01 LIKE pool_00;
INSERT pool_01 SELECT * FROM pool_00;
CREATE TABLE pool_02 LIKE pool_00;
INSERT pool_02 SELECT * FROM pool_00;
Thank you for your help.
Taking the error message verbatim:
#1146 - Table 'oerpn_survivor_us.old_data' doesn't exist.
I would assume that the issue is that the old_data table exists on some database other than the one you are current using. If so, then one fix here would be to simply scope old_data to the correct database. Your code should look something like:
CREATE TABLE db2.new_data SELECT * FROM db1.old_data;
Here db2 is your current database, which appears to be oerpn_survivor_us, and db1 should be replaced by the name of the database which contains the old_data table.

How to select content from a table into a new table in SQL stored procedures?

I was trying to write a procedure and needed to copy output_1 table into a new one.
This procedure :
BEGIN
SELECT * INTO newtable FROM output_1;
END
returns the following error :Undeclared variable: newtable
I thought it would create a new table and all its columns automatically.
How do I SELECT multiple columns of a table INTO a new table using a stored procedure?
EDIT :
In stored procedures, when you want to use a table to store data temporarily, you should consider using temporary tables.
Typically, if you try to store a table in a variable, you will get a multiple rows error ; in this case, temporary tables can replace variables.
CREATE TEMPORARY TABLE new_table AS SELECT * FROM output_1;
You cannot select into a table. You possibly intended
create table newtable as select * from output_1;
https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html

Insert into postgres table using apache drill

Is it possible to insert data into an existing table in postgres database using apache drill.something similar to
insert into Post_db.test_schema.customer_account_holder_test select customer_id,source_system_id,salutation,first_name,middle_name,last_name,legal_name,gender,identity_proof_name,identity_proof_value from hive.schema_name.customer_account_holder limit 10
In apache drill, Insert is not supported.
You can creates a new table and populates the new table with rows returned from a SELECT query. Use the CREATE TABLE AS (CTAS) statement in place of INSERT INTO. CREATE TABLE name AS query;

Create view statement returns an empty result set

I have a table called matchresults which has five columns named mresultid, playerid, seasonmatchid, rank, ratingsearned
mresultid is the primary key and playerid and seasonmatchid are foreign keys
The problem is that when I try to create a view on this table:
DROP VIEW IF EXISTS matchresults_view;
CREATE VIEW matchresults_view AS
select mresultid, playerid, seasonmatchid
from matchresults
where id = 8 ;
The query runs but it says mysql returned an empty result set. This is not true as I should be having two tuples in the result set.
What is wrong with the query?
Well, you are creating a view. This command does not return any rows. It simply creates a view which you can think of as SQL query saved under a name, so that it is later possible to use in your queries. It will still execute the underlying SQL statement.
Now you have to query the view like below, to see what it outputs:
select * from matchresults_view

MySQL views with arguments

I have been working on a pretty large database this last week. Basically I am taking an Access database and converting it to a MySQL database. I have seccessfully converted all the tables and views to MySQL. However I have a view that requires input from the user, the date. The other view is the view that will be call.
view 1 - compiled_fourweeks - needs date
view 2 - metrics_fourweeks - uses `compiled_fourweeks in query.
I was thinking of a precedure but I won't be able to reference the columns in the query.
I am kind of running out of ideas at this point.
If I understand correctly, you need to execute a view (metrics_fourweeks) that needs data from another view (compiled_fourweeks), and this last view requires input from the user.
I would go with the procedure approach:
create procedure fourWeeksData(d date)
create or replace view compiled_fourweeks
select ...
from ...
where recordDate = f -- Just an example; use whichever where clause you need
...;
select * from metrics_fourweeks;
end
If your database will be used just by a single user, your problem is solved. But if your database is meant to be used by more than one user... well, you can use temporary tables:
create procedure fourWeeksData2(d date)
drop table if exists temp_compiled_fourweeks;
create temporary table temp_compiled_fourweeks
select ...
from ...
where recordDate = f -- Just an example; use whichever where clause you need
...;
-- You will need to create the required indexes for this new temp table
-- Now replicate the SQL statement, using your new temp table
select ...
from temp_compiled_fourweeks
...;
end
Hope this helps you.