🛢️
PostgreSQL
Grokking Algorithm
  • Content
  • Environment
  • Getting Started
    • Introduction
      • What is PostgreSQL
      • Introduction to PostgreSQL sample database
    • Postgres on Windows
      • Install PostgreSQL on Windows
      • Connect to PostgreSQL database server
      • Load the sample database into the PostgreSQL database server
    • Postgres on Linux
    • Install PostgreSQL on macOS
  • Basic
    • Querying Data
      • SELECT
      • Column alias
      • ORDER BY
      • SELECT DISTINCT
    • Filtering Data
      • WHERE
      • AND
      • OR
      • LIMIT
      • FETCH
      • IN
      • BETWEEN
      • LIKE
      • IS NULL
    • Joining Multiple tables
      • Joins
      • Table aliases
      • INNER JOIN
      • LEFT JOIN
      • RIGHT JOIN
      • SELF JOIN
      • FULL OUTER JOIN
      • CROSS JOIN
      • NATURAL JOIN
    • Grouping data
      • GROUP BY
      • HAVING
    • Set Operations
      • UNION
      • INTERSECT
      • EXCEPT
    • Grouping sets, Cube, and Rollup
      • Grouping sets
      • CUBE
      • ROLLUP
    • Subquery
      • Subquery
      • ANY
      • ALL
      • EXISTS
    • Common Table Expressions
      • PostgreSQL CTE
      • Recursive query using CTEs
    • Modifying Data
      • Insert
      • Insert multiple rows
      • Update
      • Update join
      • Delete
      • Upsert
    • Transactions
      • PostgreSQL Transaction
    • Import & Export Data
      • Import CSV file into Table
      • Export PostgreSQL Table to CSV file
    • Managing Tables
      • Data types
      • Create a table
      • Select into
      • Create table as
      • Auto-increment
      • Sequences
      • Identity column
      • Alter table
      • Rename table
      • Add column
      • Drop column
      • Change column data type
      • Rename column
      • Drop table
      • Truncate table
      • Temporary table
      • Copy a table
    • Understanding PostgreSQL constraints
      • Primary key
      • Foreign key
      • UNIQUE constraint
      • CHECK constraint
      • NOT NULL constraint
    • PostgreSQL Data Types in Depth
      • Boolean
      • CHAR, VARCHAR and TEXT
      • NUMERIC
      • Integer
      • DATE
      • Timestamp
      • Interval
      • TIME
      • UUID
      • Array
      • hstore
      • JSON
      • User-defined data types
  • Advanced
  • PG-PGSQL
  • Functions
  • Adminstration
  • API
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Basic
  2. Modifying Data

Update join

PreviousUpdateNextDelete

Last updated 1 year ago

Was this helpful?

Ushbu qoʻllanmada siz boshqa jadvaldagi qiymatlar asosida jadvaldagi maʼlumotlarni yangilash uchun PostgreSQL UPDATE qoʻshilish sintaksisidan qanday foydalanishni oʻrganasiz.

Baʼzan jadvaldagi maʼlumotlarni boshqa jadvaldagi qiymatlar asosida yangilashingiz kerak boʻladi. Bunday holda siz PostgreSQL UPDATE qo'shilish sintaksisidan quyidagi tarzda foydalanishingiz mumkin:

UPDATE t1
SET t1.c1 = new_value
FROM t2
WHERE t1.c2 = t2.c2;

UPDATE bayonida boshqa jadvalga qoʻshilish uchun siz FROM bandida birlashtirilgan jadvalni belgilaysiz va WHERE bandida birlashma shartini taqdim etasiz. FROM bandi SET bandidan keyin darhol paydo bo'lishi kerak.

t1 jadvalining har bir qatori uchun UPDATE operatori t2 jadvalining har bir qatorini tekshiradi. Agar t1 jadvalining c2 ustunidagi qiymat t2 jadvalining c2 ustunidagi qiymatga teng bo'lsa, UPDATE operatori t1 jadvalining c1 ustunidagi qiymatni yangi qiymatni (new_value) yangilaydi.

PostgreSQL UPDATE JOIN misoli

Keling, PostgreSQL UPDATE qo'shilishi qanday ishlashini tushunish uchun misolni ko'rib chiqaylik. Namoyish uchun quyidagi ma'lumotlar bazasi jadvallaridan foydalanamiz:

Birinchidan, mahsulot segmentlarini, jumladan, katta hashamat, hashamat va massani saqlaydigan product_segment deb nomlangan yangi jadval yarating.

product_segment jadvalida discount ustuni mavjud boʻlib, unda maʼlum segmentga asoslangan chegirma foizi saqlanadi. Misol uchun, grand luxury segmentiga ega mahsulotlar 5% chegirmaga ega, hashamatli va ommaviy mahsulotlar mos ravishda 6% va 10% chegirmalarga ega.

CREATE TABLE product_segment (
    id SERIAL PRIMARY KEY,
    segment VARCHAR NOT NULL,
    discount NUMERIC (4, 2)
);


INSERT INTO 
    product_segment (segment, discount)
VALUES
    ('Grand Luxury', 0.05),
    ('Luxury', 0.06),
    ('Mass', 0.1);

Ikkinchidan, mahsulot ma'lumotlarini saqlaydigan product nomli boshqa jadval yarating. product jadvalida segmentlar jadvalining idiga bog'langan segment_id tashqi kalit ustuni mavjud.

CREATE TABLE product(
    id SERIAL PRIMARY KEY,
    name VARCHAR NOT NULL,
    price NUMERIC(10,2),
    net_price NUMERIC(10,2),
    segment_id INT NOT NULL,
    FOREIGN KEY(segment_id) REFERENCES product_segment(id)
);


INSERT INTO 
    product (name, price, segment_id) 
VALUES 
    ('diam', 804.89, 1),
    ('vestibulum aliquet', 228.55, 3),
    ('lacinia erat', 366.45, 2),
    ('scelerisque quam turpis', 145.33, 3),
    ('justo lacinia', 551.77, 2),
    ('ultrices mattis odio', 261.58, 3),
    ('hendrerit', 519.62, 2),
    ('in hac habitasse', 843.31, 1),
    ('orci eget orci', 254.18, 3),
    ('pellentesque', 427.78, 2),
    ('sit amet nunc', 936.29, 1),
    ('sed vestibulum', 910.34, 1),
    ('turpis eget', 208.33, 3),
    ('cursus vestibulum', 985.45, 1),
    ('orci nullam', 841.26, 1),
    ('est quam pharetra', 896.38, 1),
    ('posuere', 575.74, 2),
    ('ligula', 530.64, 2),
    ('convallis', 892.43, 1),
    ('nulla elit ac', 161.71, 3);

Uchinchidan, har bir mahsulotning sof narxini mahsulot segmentidagi chegirma asosida hisoblashingiz kerak, deylik. Buning uchun siz UPDATE qo'shilish bayonotini quyidagicha qo'llashingiz mumkin:

UPDATE product
SET net_price = price - price * discount
FROM product_segment
WHERE product.segment_id = product_segment.id;

So'rovni quyidagicha qisqartirish uchun jadval taxalluslaridan foydalanishingiz mumkin:

UPDATE 
    product p
SET 
    net_price = price - price * discount
FROM 
    product_segment s
WHERE 
    p.segment_id = s.id;

Ushbu bayonot product jadvalini product_segment jadvaliga birlashtiradi. Ikkala jadvalda ham mos keladigan bo'lsa, product_segment jadvalidan chegirma oladi, quyidagi formula asosida sof narxni hisoblab chiqadi va net_price ustunini yangilaydi.

net_price = price - price * discount;

Quyidagi SELECT bayonoti yangilanishni tekshirish uchun product jadvali ma'lumotlarini oladi:

SELECT * FROM product;

Ko'rib turganingizdek, net_price ustuni to'g'ri qiymatlar bilan yangilangan.

Ushbu qo'llanmada siz boshqa jadvaldagi qiymatlar asosida jadvaldagi ma'lumotlarni yangilash uchun PostgreSQL UPDATE qo'shilish bayonotidan qanday foydalanishni o'rgandingiz.

©

postgresqltutorial.com
output
output