Skip to content

Latest commit

 

History

History
120 lines (82 loc) · 3.23 KB

2. COLUMN_ALIAS.md

File metadata and controls

120 lines (82 loc) · 3.23 KB

Column alias

Ushbu qo'llanmada siz PostgreSQL ustun taxalluslari va so'rovlardagi ustunlarga vaqtinchalik nomlar belgilash uchun ustun taxalluslaridan qanday foydalanish haqida bilib olasiz.

Ustun taxallusi sizga SELECT bayonotning tanlangan ro'yxatidagi ustun yoki ifodaga vaqtinchalik nom belgilash imkonini beradi. Ustun taxallusi so'rovni bajarish vaqtida vaqtincha mavjud.

Quyida ustun taxallusdan foydalanish sintaksisi tasvirlangan:

SELECT column_name AS alias_name
FROM table_name;

Ushbu sintaksisda column_namega alias_name taxallus berilgan. AS kalit so'z ixtiyoriy, shuning uchun uni quyidagi tarzda o'tkazib yuborishingiz mumkin:

SELECT column_name alias_name
FROM table_name;

Quyidagi sintaksis SELECT banddagi ifoda uchun taxallusni qanday o'rnatishni ko'rsatadi:

SELECT expression AS alias_name
FROM table_name;

Ustun taxalluslarining asosiy maqsadi so'rov natijalarining sarlavhalarini yanada mazmunli qilishdir.

PostgreSQL ustun taxalluslariga misollar

Namoyish uchun quyidagi namunaviy ma'lumotlar bazasida customer jadvalidan foydalanamiz.

customer

1. Ustun misoliga ustun taxallusni belgilash

Quyidagi so'rov customer jadvaldagi barcha mijozlarning ismlari va familiyalarini qaytaradi :

SELECT 
   first_name, 
   last_name
FROM customer;

output

Agar siz last_name sarlavha nomini o'zgartirmoqchi bo'lsangiz, unga quyidagi kabi ustun taxallus yordamida yangi nom berishingiz mumkin:

SELECT 
   first_name, 
   last_name AS surname
FROM customer;

Ushbu so'rov last_name ustunning surname taxallus sifatida tayinlangan:

output

Yoki AS kalit so'zni quyidagi tarzda olib tashlash orqali uni qisqartirishingiz mumkin :

SELECT 
   first_name, 
   last_name surname
FROM customer;

2. Ifoda misoliga ustun taxallusni belgilash

Quyidagi so'rov barcha mijozlarning to'liq ismlarini qaytaradi. U ism, bo'sh joy va familiyani birlashtirib, to'liq ismni yaratadi:

SELECT 
   first_name || ' ' || last_name 
FROM 
   customer;

E'tibor bering, PostgreSQLda siz bir yoki bir nechta satrlarni bitta satrga birlashtiradigan "||" birlashtiruvchi operator sifatida foydalanasiz.

output

Chiqishdan aniq ko'rinib turibdiki, ustunning ?column? sarlavhasi mazmunli emas.

Buni tuzatish uchun siz first_name || ' ' || last_name iboraga ustun taxallusni belgilashingiz mumkin, masalan full_name:

SELECT
    first_name || ' ' || last_name AS full_name
FROM
    customer;

output

3. Bo'shliqlarni o'z ichiga olgan ustun taxalluslari

Agar ustun taxallusi bir yoki bir nechta bo'sh joyni o'z ichiga olsa, uni quyidagi kabi qo'sh tirnoq bilan o'rashingiz kerak:

column_name AS "column alias"

Masalan:

SELECT
    first_name || ' ' || last_name "full name"
FROM
    customer;

Xulosa

  • column_name AS alias_name yoki expression AS alias_name sintaksisi yordamida ustun yoki iboraga ustun taxallusni tayinlang.
  • Kalit AS so'z ixtiyoriy.
  • Boʻsh joy boʻlgan ustun taxallusni oʻrab olish uchun qoʻsh tirnoq (") dan foydalaning.