hello I would like to insert the values in a column from a select query
insert into (cmts) stat_alert
values(SELECT stat_alert.cell,stat_alert.cmts,cell_cmt.cmts FROM stat_alert`LEFT OUTER JOIN cell_cmt ON cell_cmt.`cell`=stat_alert.cell WHERE stat_alert.`cell`=cell_cmt.cell )
I have syntax error
who can help me??
You do not need VALUES keyword and round brackets:
INSERT INTO <table name> (<comma separated column list>)
SELECT <comma separated column list> FROM <table name>
Read the INSERT ... SELECT syntax from the manual.
You want to use INSERT INTO otherTable (cols) SELECT <yourColumns> FROM <yourtable> not INSERT INTO ... VALUES():
insert into stat_alert (cmts) -- list your columns here to insert into
SELECT cell_cmt.cmts -- your values to be inserted are here
FROM stat_alert
LEFT OUTER JOIN cell_cmt
ON cell_cmt.`cell`=stat_alert.cell
WHERE stat_alert.`cell`=cell_cmt.cell
It looks like you have:
The column list before the table name
One column to insert into, but three columns selected
A values keyword when none is needed in this kind of query.
Related
I would like to select different column from different row from the same table with one statement . How will I combine this to be one?
SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'
You could just use each of two current queries as subqueries in one select statement:
SELECT
(SELECT shop_lat_log FROM customers WHERE phoneNumber = '254719401837') AS shop_lat_log,
(SELECT delivery_lat_log FROM customers WHERE phoneNumber = '25472054919') AS delivery_lat_log
FROM dual;
This assumes that each of your two queries returns a single value. If not, then perhaps a UNION would be more appropriate:
SELECT
shop_lat_log AS log_value,
'shop_lat_log' AS log_type
FROM customers
WHERE phoneNumber = '254719401837'
UNION ALL
SELECT
delivery_lat_log,
'delivery_lat_log'
FROM customers
WHERE phoneNumber = '25472054919'
You can use a UNION for that:
SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
UNION
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'
Note that the second query must have the same number of columns as the first query, and the results will have the first query's column names.
So even though you're selecting the delivery_lat_log column in your second query, the results will be in the shop_lat_log column if you're fetching an associative array.
Use an SQL case statement
select case c.phone_number
when '254719401837'
then c.shop_lat_log
when '25472054919'
then c.delivery_lat_log
end as field
from customer as c
where c.phone_number in ('254719401837', '25472054919')
He needs to insert a table of values from a select query and additionally string
$query=("INSERT INTO table1 (a,b,c,d) VALUES ('a',SELECT b,c,d FROM table WHERE x=1)");
Could you as him to try the following:
INSERT INTO table1 (a,b,c,d) VALUES (SELECT 'a',b,c,d FROM table WHERE x=1)
Am having two tables inside my Database lets say Table1 and Table2, and am trying to copy the values from a specific rows(fields) in Table2 to a specific single row on Table1, The specific rows to be moved are determined by the User Input of Particular UserID of each row from Table1.
Table Structure:
Table 1: a_uid,a_FName,a_Username,a_PhoneNo,b_uid,b_FName,b_Username,b_PhoneNo......
And Table 2 Structure:
Table 2: uid,FName,Username,PhoneNo.....
Am uing the INSERT INTO .. SELECT statement, but with multiple WHERE clause but its giving me erros
INSERT INTO table1 WHERE uid='userinput1' (b_FName,b_Username,b_PhoneNo,) SELECT FName,Username,PhoneNo FROM table2 WHERE uid='userinput2';
But am getting Error
This type of clause was previously parsed. (near WHERE)
If the row in table1 already exists, you need an UPDATE .. JOIN statement instead of INSERT .. SELECT.
UPDATE table1 t1
JOIN table2 t2 ON t2.uid='userinput2'
SET t1.b_FName = t2.FName,
t1.b_Username = t2.Username,
t1.b_PhoneNo = t2.PhoneNo
WHERE t1.uid='userinput1'
If you don't know if the row in table1 already exists, you can use an INSERT .. SELECT .. ON DUPLICATE KEY UPDATE statement:
INSERT INTO table1 (uid, b_FName, b_Username, b_PhoneNo)
SELECT 'userinput1', FName, Username, PhoneNo
FROM table2
WHERE uid = 'userinput2'
ON DUPLICATE KEY UPDATE
SET b_FName = VALUES(FName),
b_Username = VALUES(Username),
b_PhoneNo = VALUES(PhoneNo)
Note that uid sould be primary keys or at least unique in both tables.
For sql-server it would be something like this:
INSERT INTO table1 (uid,FName,Username,PhoneNo)
SELECT #userinput1, FName,Username,PhoneNo
FROM table2
WHERE table2.uid=#userinput2;
I have 2 tables let say table1 & table2
table1 contains uniqueId, name1, name2, value fields
table2 contains id, uniqueName, keywords fields
table2.keyworks have comma separate names.
So, what I am trying to do is below.
select * from table1
//1> replace table1.name1 with table2.uniqueName if table2.keywords has
//table1.name1
//2> replace table1.name2 with table2.uniqueName if table2.keywords has
//table1.name2
select *,(case when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name1
when FIND_IN_SET(table.name1,table2.keywords)>0 then table1.name2 end)from table1
Try this.
I am using postgre sql and phpPgAdmin for my database.
I have a table where i added some table names like:
table_lsts
id name tablename
11 abbc table1
22 xyyz table2
33 deef table3
now in table1, table2, table3 i have multiple records having a common column named as vpvc_id.
my problem is i have to write a query which first of all get all table names from table_lsts.
then count record in those table where vpvc_id in (some ids here) etc
can anyone help please?
try this function :
CREATE OR REPLACE FUNCTION count_records (ids_lst character varying)
RETURNS integer AS
$BODY$declare
query character varying;
result integer;
BEGIN
-- First query that create a dynamic query on all tables of table_lsts
select into query 'select count(*) from (' || String_agg('select vpc_id from '||tablename,' UNION ALL ') || ') a where vpc_id in ('||ids_lst||')' from table_lsts ;
---execute dynamic query
execute query into result;
return result;
END;$BODY$
LANGUAGE plpgsql VOLATILE
And use it like this :
select count_records('2,5')