I have parent table customer id is primary key
In customer details table - customer id is reference key
I need to update negative values to customer id in child table.
In parent I have 1 customer id - 1000
In child table I have 10 records for customer 1000 - I need to update same nagative value for 10 records in customer child table. Like this I need to update whole table
I need to achieve with SQL query not with PLSQL
Ex: I tried with few case it's not working
Update customer details A set A.customer id = -1000+1
Where customer id in (
select B.customer id
from customer master
where B.customer id = A.customer id
)
It updating different values to child table for single customer
Is it possible to update every customer in child table with unquie value based on customer with single update query
Master table
Customer id name
1000 Kim
1001 June
Child table
Customer id.
1000
1001
1000
1001
I need to update 1000 and 1001 with unique I'd is this possible in single query
Related
There are clients(companies) and contacts in two tables. Both contacts and clients have their own fields. But they all have an id that is a primaryKey. In addition, there are "links" between clients and contacts - each client can be assigned a contact and vice versa. There is a third table for this, called 'assignment', which has only two fields - company id and contact id.
Suppose we have linked a contact with id 3 to a company with id 12076. Next, we have linked a contact with id 10, and the last contact with id 5. Сheck our result:
SELECT contactId FROM assignment WHERE companyId = 12076;
The result is correct -
After these steps, at some point I need to get the FIELDS of contacts associated with the company I need. There is a company 12076, I need to get all the contact fields associated with it. I made a request like this:
SELECT id, fullName FROM contacts WHERE contacts.rowid IN (SELECT contactId FROM assignment WHERE companyId = 12076);
This request displays the contacts I need and their fields. Exactly 3 contacts, but there is one problem - they are displayed in sorted order, sorted by id.
I need the contact fields to be displayed in the order in which they were added. How can i dow this? Maybe i need to use JOIN?
By default an SQLite table has a Rowid column. We can use this column in ORDER BY.
If we want the ContactId's in the order they were inserted into the table contacts we can join onto contacts and order by it's Rowid.
See [https://www.sqlitetutorial.net/sqlite-autoincrement/][1]
create table companies(id int);
create table contacts (id int);
create table assignment(companyId int,contactId int);
insert into companies values(12076);
insert into contacts values(10),(3),(5);
insert into assignment values (12076,3),(12076,10),(12076,5);
SELECT a.contactId
FROM assignment a
JOIN contacts c
on a.contactId = c.id
WHERE companyId = 12076
order by c.Rowid;
| contactId |
| --------: |
| 10 |
| 3 |
| 5 |
db<>fiddle here
I want to delete the records in the table based on 2 condition.
This is my query :
delete from customer where productid not in (1,2,3,4,5) and manufacturerid not in ( 8,10)
The problem is I am able to see customer with productid 6,7,8 etc where manufacturerid is 8 and 10.
I want to delete all the product except id 1,2,3,4,5 which has manufacturerid 8 and 10 alone but my query is not working as expected.
According to your explanation you want to delete as follows with OR and not AND. This means that will will delete where either, or both, of the conditions is true.
DELETE FROM customer
WHERE
productid NOT IN (1,2,3,4,5)
OR
manufacturerid NOT IN ( 8,10) ;
I think what you want is actually a NOT with an AND:
DELETE C
FROM Customer C
WHERE NOT (C.productid IN (1,2,3,4,5) AND C.manufacturerid IN (8,10));
I am writing a SQL statement for Oracle where there is a one to many relationship between two tables. The table Person has a foreign key to table Purchase which has a Purchase Description field.
I need to write a SELECT query that will take all the purchase records/rows and append them to each other like so
Person Table
PersonID PersonName
1 John
Purchases Table
PurchaseId (PK), PersonID(FK), PurchaseDescription
1 1 Book
2 1 Clothes
3 1 Bag
4 1 Dinner
So the output of the query would look like this
Output = 1, Book:Bag:Clothes:Dinner
The output will be one row from the one to many relationship where there are separate records for book, bag, clothes, and dinner.
Any help is appreciated. Thanks
to do this use a function called LISTAGG, like this
SELECT 'Output = '||CAST(P.PersonID AS VARCHAR(100)), LISTAGG(Pur.PurchaseDescription, ':')
FROM Person P
LEFT JOIN Purchase Pur ON P.PersonID = Pur.PersonID
GROUP BY P.PersonID
I have this query that return to me all rows for one user
$strSQL = "SELECT * FROM customer , bills , vouchers
WHERE
bills.bills_CustomerName = customer.customer_Name and
vouchers.vouchers_CustomerName = customer.customer_Name and
bills.bills_CustomerName like '%".$_POST["MyName"]."%'
";
I have the problem that one row are repeated 2 times, the customer table is related to the bills table and to the vouchers table on one FK column.
bills table :
bills_ID - bills_CustomerName - bills_Total
customer table :
customer_ID - customer_Name - customer_Tell
vouchers table :
vouchers_ID - vouchers_CustomerName - vouchers_Total
we are get
Name Total Tell
kam johin 100 0444444444
kam johin 100 0444444444
mak pop 200 0588888888
mak pop 200 0588888888
If customer to bill is one-to-many and customer to voucher is also one-to-many then you have what is sometimes known as a "chasm trap", and you will have to aggregate child values from bill and voucher before joining to customer.
Or perhaps your data model should be more like:- Customer->Bill->Voucher, in which case you need to include in the voucher table a foreign key to the Bill to which the voucher relates.
BTW you could probably use some surrogate key for customer - what happens when two different customers have the same name?
What is the best approach to display the summery of DETAILED.Fields in its master table?
E.g. I have a master table called 'BILL' with all the bill related data and a detailed table ('BILL_DETAIL') with the bill detailed related data, like NAME, PRICE, TAX, ... Now I want to list all BILLS, without the details, but with the sum of the PRICE and TAX stored in the detail table.
Here is a simplified schema of that tables:
TABLE BILL
----------
- ID
- NAME
- ADDRESS
- ...
TABLE BILL_DETAIL
-----------------
- ID
- BILLID
- PORDUCT_NAME
- PRICE
- TAX
- ...
The retrieved table row should look like this:
BILL.CUSTOMER_NAME, BILL.CUSTOMER_ADDRESS, sum(BILL_DETAIL.PRICE), sum(BILL.DETAIL.TAX), ...
Any sugguestions?
A simple GROUP BY and a LEFT JOIN should get you what you want:
Select
bill.customer_name,
bill.customer_address,
Sum(bill_detail.price),
Sum(bill_detail.tax)
From bill
Left Join bill_detail On ( bill_detail.billid = bill.id )
Group By bill.id, bill.customer_name, bill.customer_address
Make sure to group by all columns of table bill that you have in the column list of your select.