1

我有一个名为属性的表,我想获取此表中匹配“hold = 1”的所有记录,但是一个额外的表包含不同语言的属性名称我需要获取英语,或者如果不存在语言标记作为默认值

表属性:

pid     hold

属性翻译:

pid     lang_id     pname     description     isDefault

我通常使用工会获得语言

(select pname from property_translations where lang_id='en' and pid=$pid)
union
(select pname from property_translations where isDefault='Yes' and pid=$pid)
limit 1
4

4 回答 4

1
SELECT p.pid, t.pname FROM properties p LEFT OUTER JOIN translations t ON p.pid = t.pid
    WHERE p.hold=1 AND t.isDefault='Yes' AND NOT EXISTS
    ( SELECT * FROM translations ti WHERE ti.pid = p.pid AND ti.lang_id='en')
    UNION SELECT p.pid, t.pname FROM properties p 
    LEFT OUTER JOIN translations t ON p.pid = t.pid
    WHERE p.hold=1 AND t.lang_id='en';
于 2012-09-07T23:33:52.190 回答
0

尝试:

   SELECT pt.pname
     FROM property_translations AS pt
LEFT JOIN properties AS p ON p.pid = pt.pid
    WHERE pt.pid = $pid
      AND p.hold = 1
      AND pt.lang_id='en'
      AND isDefault='Yes'
于 2012-09-07T23:34:10.640 回答
0

如果你select得到你想要的结果并且你只想过滤它们hold=1,你可以简单地加入第一个表:

(select pname from property_translations t
              join properties p on (t.pid=p.pid and p.hold = 1)
              where lang_id='en' and pid=$pid)
union
(select pname from property_translations t
              join properties p on (t.pid=p.pid and p.hold = 1)
              where isDefault='Yes' and pid=$pid
              and not exists (select 1 from property_translations t2
                              where t2.pid=t.pid and t2.lang_id='en' and t2.isDefault='Yes'))

请注意,我不是在检查您原件的有效性select,只是展示如何针对第二个表添加检查。

于 2012-09-07T23:37:57.897 回答
0
SELECT p.pid pid, pname
from properties p
JOIN (SELECT t1.pid tpid, IFNULL(t2.pname, t1.pname) pname
      FROM translations t1
      LEFT OUTER JOIN translations t2
      ON t1.pid = t2.pid
      AND t2.lang_id = 'en'
      WHERE t1.isDefault = 'Yes') t
ON p.pid = t.pid
WHERE p.hold = 1
于 2012-09-08T00:29:19.603 回答