[英]Select all rows from one table and one specific value for each row from another table
[英]Select results from one table and for each row select all rows that have the same id from another table
更新:由於第一個問題已解決(謝謝,我將第一個問題保留在底部,供您查看),因此正確的連接現在可以正常進行了。 問題在於它僅拉入那些有聯系的照片。 無論是否有連接,我如何拉所有照片?
添加左聯接而不是聯接似乎會拉入所有照片,只是要在沒有任何怪異的照片上添加“連接”。
這很難解釋,但我會盡力而為。 首先,讓我為冗長的標題表示歉意,我只是想使其盡可能詳細。
這是關於在畫廊中顯示照片(user_album2表)。 每張照片將顯示“連接”(來自user_connected表)。 user_connected表的示例為:
fk_pic_id | fk_user_id | fk_user_id_conn
這些列均為INT,分別提取照片ID#,進行連接ID#的用戶和已連接用戶ID#。
在下面的代碼中看到的所有“ get_”函數都是拉用戶信息的函數(因此,如果它是“ get_profession”,它將獲得職業ID#)。
$query = mysql_query("SELECT a.pic_id, a.fk_user_id as uid, a.picture_number, a.picture_name, a.id, a.featuredphoto, c.fk_pic_id, GROUP_CONCAT(CONVERT(c.fk_user_id_conn, CHAR(8))) as cuids
FROM user_album2 a LEFT JOIN user_connected c ON a.pic_id = c.fk_pic_id GROUP BY a.pic_id ORDER BY RAND() LIMIT 40") or die(mysql_error());
while ($row = mysql_fetch_array($query)) {
$pic_id = $row['pic_id'];
$userid = $row['uid'];
$photonum = $row['picture_number'];
$photo = $row['picture_name'];
$photoid = $row['id'];
$featured = $row['featuredphoto'];
$photogid = $row['fk_photog_id'];
$modelid = $row['fk_model_id'];
$cuids = explode(',',$row['cuids']);
if (get_profession($userid) == 1) {
$modeltag = get_name($userid);
$modelpic = get_photo($userid);
$modelLink = get_profile_link($userid);
foreach ($cuids as $c) {
$cprof = get_profession($c);
if ($cprof == 2) {
$photogtag = get_name($c);
$photogpic = get_photo($c);
$photogLink = get_profile_link($c);
}
}
} elseif (get_profession($userid) == 2) {
$photogtag = get_name($userid);
$photogpic = get_photo($userid);
$photogLink = get_profile_link($userid);
foreach ($cuids as $c) {
$cprof = get_profession($c);
if ($cprof == 1) {
$modeltag = get_name($c);
$modelpic = get_photo($c);
$modelLink = get_profile_link($c);
}
}
}
$classMargin = $modeltag && $photogtag ? ' add-bot-margin':'';
$myreturn .= '<li>'
.'<div class="inner">'
.'<img border="0" alt="" src="'. $baseurl . $photo .'&w=188&h=150&zc=1" />'
.'<div class="details">'
.'<a href="'. $baseurl .'photos/'. $userid .'/'. $photoid .'/'. $photonum .'" class="img-link">view image <strong>here</strong></a>'
.'<div class="info-contain-details">';
if ($modeltag) {
$myreturn .= '<div class="details-info model-info'. $classMargin .'">'
.'<a href="'. $baseurl . $modelLink .'" class="link-image"><img border="0" alt="" src="'. $baseurl .'img.php?src=/memberpictures/'. $modelpic .'&w=30&h=30&zc=1" /></a>'
.'<div class="photo-info">'
.'<a href="'. $baseurl . $modelLink .'" class="link-view-more">view more from</a>'
.'<a href="'. $baseurl . $modelLink .'" class="link-username">'. $modeltag .'</a>'
.'</div>'
.'</div>';
}
if ($photogtag) {
$myreturn .= '<div class="details-info photog-info">'
.'<a href="'. $baseurl . $photogLink .'" class="link-image"><img border="0" alt="" src="'. $baseurl .'img.php?src=/memberpictures/'. $photogpic .'&w=30&h=30&zc=1" /></a>'
.'<div class="photo-info">'
.'<a href="'. $baseurl . $photogLink .'" class="link-view-more">view more from</a>'
.'<a href="'. $baseurl . $photogLink .'" class="link-username">'. $photogtag .'</a>'
.'</div>'
.'</div>';
}
$myreturn .= '</div>'
.'<span class="details-bg"></span>'
.'</div>'
.'</div>'
.'</li>';
}
修正:[由於某種原因,我無法為自己的一生而使職業部分正確出現。 應該是:如果照片行業的所有者是模特,則應檢查該照片的聯系=攝影者(模型= 1,攝影者= 2)。 我在'user_connected'表中有幾行顯示同一張照片,但是當它應該是一位模特和一位攝影師時,它仍顯示相同的2位用戶(均為“模特”)。]-缺少“ = ”在$ cfprof if語句中簽名,DOH!
如果您需要任何其他信息,請告訴我,一如既往,我們將不勝感激!
固定。 問題在foreach循環內,我需要有一條ELSE語句,還將photogtag / modeltag的設置為FALSE。
if ($cprof == 2 && $c != '') {
$photogtag = get_name($c);
$photogpic = get_photo($c);
$photogLink = get_profile_link($c);
} else {
$photogtag = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.