البرمجة

اختيارات SQL لاختيار المنتجات في متجر السبا

When dealing with a table like the one you described, where you have different products and their relationships (e.g., parent tours and their children), you can use SQL to select the required items based on certain conditions. Here’s how you can achieve that:

sql
-- Select all spa products SELECT * FROM Products WHERE Type = 'Spa'; -- Select all parent tours that have children with an upcoming date SELECT DISTINCT p1.* FROM Products p1 JOIN Products p2 ON p1.ID = p2.ParentID WHERE p1.Type = 'Tour' AND p2.Date > NOW();

In the first query, we simply select all products where the Type is ‘Spa’. In the second query, we join the Products table with itself (aliased as p1 and p2) on the condition that p1.ID (the parent tour) matches p2.ParentID (the child tour’s parent ID). We then select the distinct parent tours (p1) where the child tour (p2) has a date greater than the current date (NOW()).

This way, you’ll get the desired result: all spa products and all parent tours that have children with an upcoming date.

المزيد من المعلومات

عند التعامل مع جدول يحتوي على علاقات تسمى “العلاقات الفرعية”، حيث تكون هناك صلة بين منتج رئيسي (parent) ومنتج فرعي (child)، يمكنك استخدام ال SQL لاستعراض واختيار البيانات بناءً على شروط معينة. فيما يلي كيفية تحقيق ذلك:

sql
-- اختيار جميع منتجات السبا SELECT * FROM Products WHERE Type = 'Spa'; -- اختيار جميع الجولات الرئيسية التي لديها أطفال لهم موعد قادم SELECT DISTINCT p1.* FROM Products p1 JOIN Products p2 ON p1.ID = p2.ParentID WHERE p1.Type = 'Tour' AND p2.Date > NOW();

في الاستعلام الأول، نقوم ببساطة باختيار جميع المنتجات حيث يكون نوعها ‘Spa’. أما في الاستعلام الثاني، فإننا نقوم بدمج جدول Products مع نفسه (مع اسم مختصر p1 و p2) بناءً على شرط أن p1.ID (الجولة الرئيسية) يتطابق مع p2.ParentID (هوية والد الجولة الفرعية). ثم نقوم باختيار الجولات الرئيسية المميزة (p1) حيث تكون للجولة الفرعية (p2) تاريخ أكبر من تاريخ اليوم (NOW()).

بهذه الطريقة، ستحصل على النتيجة المطلوبة: جميع منتجات السبا وجميع الجولات الرئيسية التي لديها أطفال لهم موعد قادم.

مقالات ذات صلة

زر الذهاب إلى الأعلى

المحتوى محمي من النسخ !!