你好,我有一个数据库,里面有2个表,一个是categoryTable,另一个是userMenuTable,categoryTable目前有两列, categoryId 和 categoryTitle 它目前拥有2行数据,categoryId = 1 和 2 ,categoryTitles = 新闻和博客,在 userMenuTable 中,我记录了用户选择的类别,该表有3列, menuEntryId , categoryId 和 cookieId ,此表记录了哪个cookie选择了哪个类别,然后ID将运行这些查询,
第一个查询,获取用户选择的类别
function getMenu($cookieId) {
$this->db->select('*');
$this->db->from('categoryTable');
$this->db->join('userMenuTable', 'categoryTable.categoryId = userMenuTable.categoryId', 'left');
$this->db->where('userMenuTable.cookieId', $cookieId);
$query = $this->db->get();
return $query->result_array();
}
下一个查询获取没有分配cookieId的所有类别,
function getAllMenus($cookieId) {
$sql ="SELECT categoryTable.categoryTitle, categoryTable.categoryId, userMenuTable.cookieId, userMenuTable.menuEntryId, categoryTable.categoryOnline,
categoryTable.categoryIsSpecial, categoryTable.categoryDateCreated, categoryTable.categorySlug, categoryTable.dashboardUserId, categoryTable.categoryAbstract
FROM categoryTable LEFT JOIN userMenuTable
ON categoryTable.categoryId = userMenuTable.categoryId
UNION ALL
SELECT categoryTable.categoryTitle, categoryTable.categoryId, userMenuTable.cookieId, userMenuTable.menuEntryId, categoryTable.categoryOnline,
categoryTable.categoryIsSpecial, categoryTable.categoryDateCreated, categoryTable.categorySlug, categoryTable.dashboardUserId, categoryTable.categoryAbstract FROM categoryTable RIGHT JOIN userMenuTable
ON categoryTable.categoryId = userMenuTable.categoryId
WHERE userMenuTable.cookieId = NULL";
$ query = $ this-> db-> query($ sql); return $ query-> result_array(); }
但是这会返回一个看起来像这样的数组,
[0] => Array
(
[categoryId] => 1
[categoryTitle] => blog
[categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p>
[categorySlug] => blog
[categoryIsSpecial] => 0
[categoryOnline] => 1
[categoryDateCreated] => 1265123745
[dashboardUserId] => 0
[menuEntryId] => 5
[cookieId] => bang4b696152b4869
)
[1] => Array
(
[categoryId] => 8
[categoryTitle] => News
[categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w
[categorySlug] => news
[categoryIsSpecial] => 0
[categoryOnline] => 1
[categoryDateCreated] => 1265283717
[dashboardUserId] => 0
[menuEntryId] => 6
[cookieId] => bang4b696152b4869
)
[2] => Array
(
[categoryTitle] => blog
[categoryId] => 1
[cookieId] => bang4b696152b4869
[menuEntryId] => 5
[categoryOnline] => 1
[categoryIsSpecial] => 0
[categoryDateCreated] => 1265123745
[categorySlug] => blog
[dashboardUserId] => 0
[categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p>
)
[3] => Array
(
[categoryTitle] => News
[categoryId] => 8
[cookieId] => bang4b696152b4869
[menuEntryId] => 6
[categoryOnline] => 1
[categoryIsSpecial] => 0
[categoryDateCreated] => 1265283717
[categorySlug] => news
[dashboardUserId] => 0
[categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w
)
)
不知何故,我需要构建一个返回 categoryTable 中所有类别的查询,并检查其中的Id是否与 userMenuTable 中的一个匹配 cookieId 与用户的匹配,然后返回一个数组,以便我可以像这样循环它,
if(isset($mainMenu)) {
//die(print_r($mainMenu));
foreach ($mainMenu as $k => $v) {
if($v['menuEntryId'] == '') {
echo "<li class='menuItem'>
<a href='".base_url()."welcome/getContent/$v[categoryId]' class='navLink' id='$v[categoryTitle]'>".$v['categoryTitle']."</a>
</li>";
} else {
echo "<li class='menuItem'>
<a href='".base_url()."welcome/getContent/$v[categoryId]' class='saved navLink' id='$v[categoryTitle]'>".$v['categoryTitle']."</a>
</li>";
}
}
} else {
// do something else
//echo "here";
}
答案 0 :(得分:0)
在此处运行左连接时,您将在左侧列中获取所有categoryTable项,在userMenuTable列中获取空值。您可以使用此功能,因为尚未保存的类别将包含userMenuTable列的空值。
SELECT categoryTable.*, userMenuTable.*
FROM categoryTable LEFT JOIN userMenuTable ON categoryTable.categoryID = userMenuTable.categoryID
WHERE userMenuTable.cookieID == '{$cookieID}' OR userMenuTable.cookieID IS NULL;
这可能会给你一个看起来像这样的结果:
[0] => Array
(
[categoryId] => 1
[categoryTitle] => blog
[categoryAbstract] => <p>asdsdsadasdsadfdsgdgdsgdsgssssssssssss</p>
[categorySlug] => blog
[categoryIsSpecial] => 0
[categoryOnline] => 1
[categoryDateCreated] => 1265123745
[dashboardUserId] => 0
[menuEntryId] => 5
[cookieId] => bang4b696152b4869
)
[1] => Array
(
[categoryId] => 8
[categoryTitle] => News
[categoryAbstract] => <p>The world at Bang Marketing moves fast, keep up to date w
[categorySlug] => news
[categoryIsSpecial] => 0
[categoryOnline] => 1
[categoryDateCreated] => 1265283717
[dashboardUserId] => 0
[menuEntryId] => 6
[cookieId] => bang4b696152b4869
)
[2] => Array
(
[categoryId] => 9
[categoryTitle] => Extra
[categoryAbstract] => <p>Another category you haven't saved
[categorySlug] => extra
[categoryIsSpecial] => 0
[categoryOnline] => 1
[categoryDateCreated] => 1265283717
[dashboardUserId] => 0
[menuEntryId] => NULL
[cookieId] => NULL
)
)