I have a situation where I need to insert
into multiple tables and the last table will depend on the table before it with some extra list of items. I am using pg with node and javascript.
Table 1:
insert into col1, col2 values (1, 2) return col1 as table1_id
Table 2:
insert into col1 col2 values ( table1_id, val1),
(table1_id, val2),
.
.
.
.
(table1_id, val_n)
I have seen here some who have done that but with just 1 row in the last table but I will have a list of items as val1....valn... to be merged with the id generated from the first insert in table1 and added to a list that I have. This is going to be a relationship table with a list of items that belong to a record in table1.
Any clues would be appreciated..
Thanks
I think you should create a 'dao' layer in postgres. An Postgres function with all parameters and return what you should have in frontend.
And this postgres function insert all row what you want.
Like this you can manage your problem:
CREATE OR REPLACE FUNCTION function_what_i_want(val1 integer, val2 integer, my_argument integer[])
RETURNS my_schema_and_table[] AS
$BODY$
DECLARE
result my_schema_and_table[];
BEGIN
insert into table1(col1,col2,col3) VALUES (nextval('seq1'),val1, val2);
insert into table2(c1,c2)
select currval('seq1'),m.c2
from unnest(my_argument) m(c2);
END;
I don't know your project. If you doesn't want search on this tables, perhaps is better store this lists in json.
Related
I am creating an application in NodeJs.
In this I have a SQL Server database in which I save data and perform queries.
One of these queries in particular doesn't work for me, I think it's due to my inexperience with SQL.
The step table stores the steps performed by a subject throughout history, so it can have one or more steps (one or more rows in the table with the same contact_id but different dates->(FECHA)).
I am using this query, but it returns empty.
SELECT FECHA, id_contacto FROM step WHERE id_contacto = 680
AND FECHA = (SELECT MAX(FECHA) FROM step).
What I want to obtain is the last step of the subject 680, that is, for all the possible steps of the contact 680, obtain the one where DATE is the largest.
this is the structure of the table.
Lets assume we have table with two records
id_contacto=1 FECHA='2022-09-30'
id_contacto=680 FECHA='2021-10-31'
Your subquery
SELECT MAX(FECHA) FROM step
returns '2022-09-30' (maximum date in the table)
then your query is
SELECT FECHA, id_contacto FROM step WHERE id_contacto = 680
AND FECHA ='2022-09-30'
table does not contain the required data, so resultset is empty
I want to store this list of lists but I don't know how to store it in MySQL
list[x][y] the items in this list contains {li:[{x:x,y:y}] , pos:{x:y}}
list[x][y].li[z].x
list[x][y].li[z].y
list[x][y].pos.x
list[x][y].pos.y
for better undersing, please have a look at this
edited:
is this right? so this means i will only have 2 tables?
You should use a separate table with sub-lists that have a column parent_id, and then a third table with actual list items of low level lists.
The query for this will look like this:
SELECT li.x, li.y, sl.id
FROM li_items li
JOIN sub_lists sl on li.list_id = sl.id
JOIN lists l on sl.parent_id = l.id;
The process of converting the result rows depends on if you use some ORM or plain mysql client.
You could also store it as a JSON, as deleted answer has suggested, but than you wan't be able to query specific items without selecting and parsing all the lists. You could also use MySQL's JSON column, but In your case having separate tables seems to be better
I've searched into the documentation of data tables of how one can add data to the table here
https://datatables.net/examples/data_sources/index.html but no where I could find a way to insert data into a single cell for a given column. For example, I need sth like:
"columns": [ {target (0), "value which will be inserted"} ]
There is a way to inser data from an array but each section of the array contains values for the whole row ( see here https://datatables.net/examples/data_sources/js_array.html) but I need to insert data into different cells in respect with the columns since I don't know the column labels initially. This is because the data will be in json format and first I need to extract the unique dates for each json object. These will be my column headers. And then for each of the objects based on its date I need to put it into the relevant date column. So the logic should be sth like:
if this date column (from the table) == json object date then put it there
Thanks
It seems like after a lot of researching this feature is not available in DataTables. So that's why I've solved the problem with jQuery. Using the code below I can iterate through a column and insert values in it.
//change 1 to the column at hand
$("#table tr > :nth-child(1)").each(function(index) {
//skip the column header
if (index > 0){
//insert value
$(this).html("<span class='fixed-size-square'> value to be inserted");
}
});
Recently i came across one problem of finding N Level Child from tables.
Let say we have following list of tables:
Table: **Country**
PKCountryID
CountryCode
CountryName
Table: **State**
PKStateID
FKCountryID
StateCode
StateName
Table: **Area**
PKAreaID
FKStateID
AreaCode
AreaName
Table: **Customer**
PKCustomerID
FKAreaID
CustomerCode
CustomerName and so on....
Here we need to find a way by which we can get N level Treeview with All Columns
Just Like
>Country
Country Table Column
Foreign Key Column of Country Table(Here : FKStateID)
>State
State Table Column
Foreign Key Column of State Table
and so on till N Level.
Is there any solution?
You can basically create the hierarchy using a CTE. If you build hte CTE properly, it will load as many levels as you need. However, I've built these before and you must be very careful in writing the code and how you write it or else performance may suffer.
Do try to use CTE to grab the information and determine if that will fit your need.
I have a project that consist of a function that generate a table based on some filter. Then, I found a problem that when some combination of some filter are return nothing so, when it rendered in browser as a table is less than number of combination.
To achieve it, I want to give "-" mark to the loss record that not saved in the table.
My first approach is check one-by-one the array with the filter and if the condition is match to 'loss rows', it will push array untill all missing record in result is filled with '-'. But, it'll dealing with time because, the last must be sorted again and then to be constructed into the wanted table.
Sample schema of main table "A":
amount_of_product, id_product, id_people, id_place.
The primary key columns are:
id_product, id_people, id_place.
The content of the main table is:
23456, book-a, 1, aa
5678, book-b, 1, cc
2587, book-b, 1, aa
The source query will be something like:
select * from A
where
id_product in ('book-a', 'book-b')
and id_people in ('1') and id_place in ('aa', 'bb', 'cc')
order by id_product, id_place
Then, we want to show in a table view (html) all combinations of all selected filters. 6 rows should be rendered, with empty cells shown as "-".
Sample result table:
Can I do it efficiently or are there any solution to this?
Thanks in advance.
Thanks for some advice above.
I've try to use crosstab function in postgresql to produce the pivot table but I can't produce for more complex table.
So, I use another approach to retrieve all datas based on combination row-coloumn gradually (example query from id_place 'aa', id_people '1', id-product 'aa' etc) and contruct each table head row and coloumn then contruct the data.
Regard