A表: 關鍵字段 id(欄位id) attr_type(欄位類別 只擷取是1)
15 1
14 0
13 1
12 1 可能會有更多
B表: 關鍵字段 a_id(欄位id) in_id(同A表ID 關聯)shop_id(擷取依據欄位 id)
400 15 15
399 15 15
388 14 15-----
387 14 15-----這兩組不做組合 以為A表中 type是0
386 13 15
385 13 15
384 12 15
383 12 15 可能會有更多是1的屬性 也可能就一組
384 12 14
383 12 14
382 12 14
381 12 14
380 12 14 可能會有更多
C表:關鍵字段 id(欄位id) shop_id(同B表shop_id 關聯)attr_id(迴圈出來的數值) 以下是我想得到的結果
1 15 400|386|384
2 15 400|386|385
3 15 400|387|384 如果此組已存在,就跳過 如果不存在 往下自動迴圈出來寫入
意圖 我給予ID 15的指令 從AB表擷取資料 產生到C表 如果原先已存在,就忽略不產生不存在的產生
比如我想迴圈shop_id 15的這個依據id 產生到C表 求代碼
以下是我寫的部分sql 後面不會了 求救
$sql = "SELECT b.a_id ". "FROM " .$GLOBALS['ecs']->table('B'). " AS b ". "LEFT JOIN " .$GLOBALS['ecs']->table('A'). " AS a ". "ON a.id IN(b.in_id) ". "WHERE b.shop_id = '15' AND a.attr_type = '1' " . "ORDER BY b.a_id DESC"; $row = $GLOBALS['db']->getAll($sql);
回複討論(解決方案)
沒看明白結果是怎麼來的,猜測一下
create temporary table A (id int, attr_type int);insert into A values (15, 1),(14, 0),(13, 1),(12, 1); create temporary table B (a_id int, in_id int, shop_id int);insert into B values (400, 15, 15),(399, 15, 15),(388, 14, 15) ,(387, 14, 15),(386, 13, 15),(385, 13, 15),(384, 12, 15) ,(383, 12, 15),(384, 12, 14),(383, 12, 14) ,(382, 12, 14),(381, 12, 14),(380, 12, 14);select shop_id, group_concat(a_id) as attr_id from A, B where A.id=B.in_id and A.attr_type=1 group by shop_id, B.in_id
shop_id attr_id 14 384,383,382,381,380 15 384,383 15 386,385 15 400,399
結果就是 我瀏覽器給與 ....&shop_id=15 這個格局 php收到指令 從b表擷取shop_id為15的資料 然後從b表隔離掉 attr_type 不是1的資料
擷取到的應該是 以下幾組
B表: 關鍵字段 a_id(欄位id) in_id(同A表ID 關聯)shop_id(擷取依據欄位 id)
400 15 15
399 15 15
386 13 15
385 13 15
384 12 15
383 12 15
然後歸類 以 in_id 區分
400 15 15
399 15 15
*********************************************************************************************************************一個分組
386 13 15
385 13 15
*********************************************************************************************************************一個分組
384 12 15
383 12 15
*********************************************************************************************************************一個分組
然後把 前面的 a_id 欄位資料做組合變化 寫入到c表 形成 400|386|384 這種
C表:關鍵字段 id(欄位id) shop_id(同B表shop_id 關聯)attr_id(迴圈出來的數值) 以下是我想得到的結果
1 15 400|386|384
2 15 400|386|385
C表 id 為 auto屬性 shop_id 為我給與的id attr_id 就是組合出來的變化結果
結果就是 我瀏覽器給與 ....&shop_id=15 這個格局 php收到指令 從b表擷取shop_id為15的資料 然後從b表隔離
寫錯了 應該是從A表帥選
結果就是 我瀏覽器給與 ....&shop_id=15 這個格局 php收到指令 從b表擷取shop_id為15的資料 然後從 A表隔離
select in_id, shop_id, group_concat(a_id) as attr_id from A, B where A.id=B.in_id and A.attr_type=1 and shop_id=15 group by B.in_id
in_id shop_id attr_id 12 15 384,383 13 15 386,385 15 15 400,399
select in_id, shop_id, group_concat(a_id) as attr_id from A, B where A.id=B.in_id and A.attr_type=1 and shop_id=15 group by B.in_id
in_id shop_id attr_id 12 15 384,383 13 15 386,385 15 15 400,399
版主,我已經自己寫好了,謝謝你啊
大神,請您幫個忙,怎麼把這個表裡的資料按照我的要求隔離出來
img_id 標示為1的 goods_id 261 attr_id 區分組的依據
最終的要求就是 輸出
a= '584,583,582,581,580,579'
b= '578,577,576,575,574,573'
572和495不輸出 因為img_id 不等於一 謝謝了
select goods_id, attr_id, groub_concat(goods_attr_id) from tbl_name where img_id=1 group by attr_id
$a = array('571','570');$b = array('569','568','567','566','565','564');$c = array('563','562','561','560','559');foreach(combineDika($a,$b) as $v){$r[] = join('|', $v);}$attr_all = join(',', $r);$attr_all = explode(',', $attr_all);foreach ($attr_all as $list){ $sql = "SELECT goods_attr " . "FROM " .$GLOBALS['ecs']->table('products') . "WHERE goods_attr = '".$list."' "; $check_attr = $GLOBALS['db']->getOne($sql);$value = 'ECS000261g_p'.rand(10000,99999);if (empty($check_attr)) { $sql = "INSERT INTO " . $GLOBALS['ecs']->table('products') . " (goods_id, goods_attr, product_sn, product_number) VALUES ('261', '".$list."', '$value', '10000')"; $GLOBALS['db']->query($sql); }}
這是我後面執行的代碼
用個2B方法讀取出來了
謝謝版主
我現在結貼