nasıl iki tablo karşılaştırmak için mysql başka bir değere sahip alanların isimlerini?

4 Cevap php

İki tablo var

  1. table_school

    school_open_time|school_close_time|school_day
    8:00 AM         | 9:00PM          | Monday
    10:00 AM        | 7:00PM          | Wednesday
    
  2. table_college

     college_open_time|college_close_time|college_day    
     10:00 AM         | 8:00PM           | Monday
     10:00 AM         | 9:00PM           | Tuesday
     10:00 AM         | 5:00PM           | Wednesday
    

Şimdi bugüne göre school_open_time school_close time, college_open_time ve college_close_time seçmek istediğiniz (anlamı college_day=school_day=today), ve bir tablonun herhangi bir özel gün için hiçbir satır varsa (LEFT JOIN, ben kullanmayı düşünüyorum) ayrıca o boş alanı gösterir.

Bu benim için en iyi ve optimize sorgu tavsiye edin.

UPDATE:

açık bir zaman ve okul için yakın bir zaman varsa o zaman college_open_time ve college_close_time school_open_time ve school_close_time olarak (veritabanı doldurulması için değil, sadece geri dönmek) iade edilmelidir. ve her zaman belirli bir gün için college_open_time ve college_close_time olmalıdır


i sorgu aşağıda kullanıyorum

 SELECT college_open_time,college_close_time ,school_open_time,
        school_close_time  FROM tbl_college
 LEFT JOIN tbl_school ON school_owner_id=college_owner_id 
 WHERE college_owner_id='".$_session['user_id']."' AND
 college_day='".date('l',time())."'";

Orada table_school belirli bir günün hiçbir satır olduğu, ANCAK zaman (sol boş değeri olan bazı değer ve sağ eli olan) tek satır dönmek sol tarafta (college_open_time, college_close_time) üzerinde aynı değer ve 6 boş satır ile ekran yedi satır Sağ taraftaki (school_open_time ve school_close_time)

i need only one row when both table have a row of a given day

ancak sorgu yukarıda kullanan school_owner_id 50 table_school karşılık yalnızca ilk satırını almak (izin), bu adı school_day durumunu göremiyor günü verilmelidir


More UPDATE @37Stars

There is a little bit problem also Dear, datatype of school_close_time and school_open time is TIME type whereas datatype of college_open_time and college_close_time is VARCHAR type. i used below code given by you but i modified a bit and i m getting close to result,

i aşağıdaki kod segmentinde IFNULL yazmak zorunda nerede ama şimdi söyle

* IFNULL(TIME_FORMAT()) Or TIME_FORMAT(IFNULL())*

SELECT TC.owner_id,college_open_time AS collegeOpen, 
       college_close_time AS collegeClose, 
TIME_FORMAT(school_open_time, '%h:%i %p' ) AS schoolOpen,       
TIME_FORMAT(school_close_time, '%h:%i %p' ) AS schoolClose
FROM tbl_college TC
LEFT JOIN tbl_school  TS ON TS.owner_id = TC.owner_id
AND TC.college_day = TS.school_day
WHERE college_day = DATE_FORMAT(NOW(),'%W')

Solution

Teşekkürler 37stars, ur dahi, IFNULL ve ideo için thanx

i m writing OPTIMUM AND BEST QUERY

SELECT TC.owner_id,college_open_time AS collegeOpen,college_close_time AS 
    collegeClose, IFNULL(TIME_FORMAT(school_open_time, '%h:%i %p'),college_open_time) 
    AS schoolOpen,IFNULL(TIME_FORMAT(school_close_time, '%h:%i %p',college_close_time)
    AS schoolClose FROM tbl_college TC LEFT JOIN tbl_school TS 
    ON TS.owner_id = TC.owner_id AND TC.college_day = TS.school_day 
    WHERE college_day = DATE_FORMAT(NOW(),'%W') 
    FROM tbl_storecalendar TS LEFT JOIN tbl_delivery_hours TD 
    ON TD.store_id = TS.store_id 
    AND TD.del_day = TS.dayName WHERE dayName = DATE_FORMAT( NOW( ) , '%W' )

4 Cevap

Sen JOIN yan tümcesi için school_day = college_day eklemeniz gerekir.

SELECT college_open_time, college_close_time, school_open_time, school_close_time
FROM dbo.tbl_college AS TC
    LEFT JOIN dbo.tbl_school AS TS ON TS.owner_id = TC.owner_id 
    AND TS.school_day = TC.college_day
WHERE TC.owner_id = 1 
    AND college_day = 'tuesday'

Bir TAM DIŞ JOIN istiyorum, ama ne yazık ki bu MySQL desteklemiyor. Neyse ki, orada kullanarak katılmak bir sol ve bir sağ katılmak birleştirerek bir geçici çözüm UNION ALL:

Güncelleme: OP'ın güncellenmiş soruyu cevaplamak için sorgu değiştirildi.

SELECT
    COALESCE(school_day, college_day) AS day,
    COALESCE(school_open_time, college_open_time) AS school_open_time,
    COALESCE(school_close_time, college_close_time) AS school_close_time,
    COALESCE(college_open_time, school_open_time) AS college_open_time,
    COALESCE(college_close_time, school_close_time) AS college_close_time
FROM (
    SELECT * FROM table_school LEFT JOIN table_college ON school_day = college_day
    UNION ALL
    SELECT * FROM table_school RIGHT JOIN table_college ON school_day = college_day
    WHERE school_day IS NULL
) AS T1

Ben aşağıdaki yapıda iki tablo için DB yapısı değişen öneriyoruz:

table institutions:
institution | institution_id
table times:
institution_id | day | open_time | close_time

You could easily put your two existing tables into the new times table, i.e.
INSERT INTO times(institution, day, open_time, close_time)
SELECT 'School', school_day, school_open_time, school_close_time
FROM table_school

And then getting the query results is easy:
SELECT i.institution, t.open_time, t.close_time
FROM times t
LEFT JOIN institutions i on t.institution_id=i.institution_id
WHERE day='Wednesday'

Bir sorguda bu başarmak istiyorsanız, bu gibi araya sonuçlarınızı koymak için UNION kullanmak gerekir:

SELECT school_open_time AS openTime,`int` AS school_close_time
    FROM table_school WHERE school_day=DATE_FORMAT(NOW(),'%W')
UNION
SELECT college_open_time AS openTime,`int` AS college_close_time
    FROM table_college WHERE college_day=DATE_FORMAT(NOW(),'%W');

DATE_FORMAT(NOW(),'%W') haftanın geçerli güne dönüştürür nerede.

Ancak, bu yöntem benim için bütün o büyük görünmüyor. Her şeyden önce, bu bir kayıp kaydının durumda sizin için belirlenen sonucu içine NULL yöntemleri koyarak idare olmaz. Sizin için bunu yapmak için bazı IF / ELSE ifadeleri ekleyebilirsiniz, ancak tüm dürüstlük, ben muhtemelen sadece PHP kodu iki ayrı sorgu yapmak istiyorum. Bu şekilde, özellikle her biri ele ve oradan daha kolay sonuçlarınızı kontrol edebilirsiniz.