Jump to content
php.lv forumi

JOIN


anonīms

Recommended Posts

Lieta tāda, ka ir skripts 'Jaunākais forumā'

 

$jaunakas_atb = mysql_query ("SELECT id, autors, teksts, topika_id, kategorijas_id FROM forums_atbildes ORDER BY id DESC LIMIT 5");
while($pedatbildes = mysql_fetch_array($jaunakas_atb))
{
................
}

 

bet sanāk tā, ka viņš tagad ņem atbildes, bet vēlos arī, lai tiktu ņemti dati no tabulas forums_topiki un tad oder by datums desc.

 

To var panākt ar JOIN vai UNION?

Un ja varētu, tad vēl mazu piemēru lūgums..

Edited by anonīms
Link to comment
Share on other sites

SELECT fa.id, fa.autors, fa.teksts, fa.topika_id, fa.kategorijas_id, ft.title FROM forums_atbildes fa left join foruma_topiki ft on ft.id = fa.topika_id  ORDER BY fa.id DESC LIMIT 5

Link to comment
Share on other sites

$jaunakas_atb = mysql_query ("SELECT fa.id, fa.autors, fa.teksts, fa.topika_id, fa.kategorijas_id FROM forums_atbildes fa left join forums_topiki ft on ft.id = fa.kategorijas_id ORDER BY fa.id DESC LIMIT 5") or die(mysql_error());

 

uztaisot print_r, rādas tikai dati, ko izvelk no forums_atbildes

 

 

 

Array ( [0] => 126 [id] => 126 [1] => aLT [autors] => aLT [2] => english coming soon [teksts] => english coming soon [3] => 34 [topika_id] => 34 [4] => 2 [kategorijas_id] => 2 )
Link to comment
Share on other sites

Tapēc, ka no izvelc datus tikai no forums_atbildes (fa) tabulas. Skaties daļu no Select līdz From. Tur pievieno ko vien vēlies no forums_topiki (ft) tabulas. Piemēram ft.topic_title

Link to comment
Share on other sites

$jaunakas_atb = mysql_query ("SELECT fa.id, fa.autors, fa.teksts, fa.topika_id, fa.kategorijas_id FROM forums_atbildes fa left join forums_topiki ft on ft.kategorijas_id, ft.autors, ft.nosaukums,  ft.kategorijas_id = fa.kategorijas_id ORDER BY fa.id DESC LIMIT 5") or die(mysql_error());

 

izlec errors par sintaksi.

Link to comment
Share on other sites

A kāpēc uzreiz bāz to visu uzreiz aiz left join on [tavi lauki] ? Ja viņiem ir jābūt uzreiz aiz Select un pirms From ?

 

SELECT
    fa.id, fa.autors, fa.teksts, fa.topika_id, fa.kategorijas_id,
    ft.autors, ft.nosaukums,  ft.kategorijas_id 
FROM forums_atbildes fa 
left join forums_topiki ft on ft.kategorijas_id = fa.kategorijas_id 
ORDER BY fa.id DESC LIMIT 5

Edited by Vebers
Link to comment
Share on other sites

$jaunakas_atb = mysql_query ("SELECT fa.id, fa.autors, fa.teksts, fa.topika_id, fa.kategorijas_id, fa.laiks, ft.autors, ft.kategorijas_id, ft.laiks 
FROM forums_atbildes fa left join forums_topiki ft on ft.laiks = fa.laiks 
ORDER BY fa.laiks DESC LIMIT 5 ") or die(mysql_error());

 

 

bet lauki no ft rādas tukši

 

 

Array ( [0] => 126 [id] => 126 [1] => aLT [autors] => [2] => english coming soon [teksts] => english coming soon [3] => 34 [topika_id] => 34 [4] => 2 [kategorijas_id] => [5] => 1205771929 [laiks] => [6] => [7] => [8] => )
Link to comment
Share on other sites

$testee = mysql_query("SELECT * FROM forums_topiki LEFT JOIN forums_atbildes ON forums_atbildes.laiks=forums_topiki.laiks ORDER BY forums_topiki.laiks  DESC LIMIT 5  ") or die(mysql_error());
$testee = mysql_fetch_array($testee);

 

print_r($testee);

 

Vot kādēļ viņš neizvelk datus no forums_atbildes tagad?

 

* - visu taču vajadzētu

Edited by anonīms
Link to comment
Share on other sites

×
×
  • Create New...