mysql IF EXISTS

3 Cevap php

Bu tabloyu var

UMS

id_attribute    value   order
1                MB      1
1                Gb      2
1                TB      3
...

ve bu tablo

ATTRIBUTE_VALUE
id    id_attribute          value  name     ums
 1         1                 50     hdd     GB
 2         1                 100    hdd     TB
 3         2                 15.00  price   NULL

ve i id_attribute = 1 ve UMS.order tarafından exist (UMS.value = ATTRIBUTE_VALUE.ums) sonra sipariş sonunda eğer nereden ATTRIBUTE_VALUE seçmek istiyorsanız ATTRIBUTE_VALUE.value tarafından grup

çıkış için örnek:

50 GB
100 Tb

   and must to appear 
    15.00   !!! here is the problem because in my UMS table i don't have UMS for price

    but it doesn't appear

3 Cevap

Eğer sorunuzu açıklığa sonra güncelleyin - Böyle bir şey deneyin:

SELECT T1.*
FROM ATTRIBUTE_VALUE T1
LEFT JOIN UMS T2
ON T1.id_attribute = T2.id_attribute AND T1.ums = T2.value
ORDER BY T2.order, T1.value

Ama T1.value 1000 büyükse, bu başarısız olur unutmayın. Onları sipariş vermeden önce aynı tip tüm birimleri dönüştürmek için daha iyi olabilir.

Sorgunun sonucu:

id  id_attribute  value  name    ums
3   2             15.00  price     
1   1             50     hdd     GB
2   1             100    hdd     TB

Örneğin, bu gibi mysql de bir koşullu sipariş yapabilirsiniz:

user 
id    name

select *
from user 
order by (case when id <5 then id else name end) 

However, you've got two tables, you need yo join them, I'm still not sure if you can get what you need this way. Also, you can't order with DESC on one branch and ASC on another.

Finlay benim kendi cevap almak var:

bu kodu:

$nr_ordine=0;
$virgula="";
$ordine="valoare";
$ordine2="FIELD(unitate_masura,";
$sql_ums=mysql_query("select * from atribute_masura where id_atribut='".$exe_atribut['id']."' order by ordine asc");
while($exe_ums=mysql_fetch_array($sql_ums))
{
$nr_ordine++;
if($nr_ordine>1)
{
$virgula=",";
};


$ordine2.=$virgula."'".$exe_ums['valoare']."'";
};
$ordine2.=")";
if($ordine2!="FIELD(unitate_masura,)")
{
$ordine=$ordine2;
};


$s_q0=mysql_query("select * from atribute_cautare where id_atribut='".$exe_atribut['id']."' group by valoare order by $ordine asc") or die (mysql_error());        
while($s_q=mysql_fetch_array($s_q0))
{

...
};

Isimler benim soru ile karşılık değil ama bu fikir order by FIELD (ums,'kb','mb','gb','tb')