iT邦幫忙

0

mysql 如何故意讓資料重複列印

我用mysql+jsp
我想印客戶資料標籤
有些客戶我要多印幾張
但我現在都只能一個客戶印一張
EX:
A客戶-1張標籤
B客戶-1張標籤
但我想要做的是可以指定張數
EX:
A客戶-2張標籤
B客戶-5張標籤
標籤印出來就是
A,A,B,B,B
可是一直失敗....
想請教大家
index.jsp


<%@ include file = "db.jsp"%> 
<!DOCTYPE html>
<html>
   <body>
<div class="container">
  
  <form action = "index.jsp" method = "post">
   <center ><input type=checkbox name='b' value="A0000">A customer</input>
     <input type="text" name="num" id="num" value="0"/>
	<input type=checkbox name='b' value="A0001">B customer</input>
     <input type="text" name="num2" id="num2" value="0"/>
    <input type = "submit" name = "index" value = "送出" > 
  </form>
</div>
<%   
      String[] b=request.getParameterValues("b");
     
        int i;
         if(request.getParameter("index") != null && b!=null)
           {  sql = "select* from send where   "; 
           //////
            int number=Integer.parseInt(request.getParameter("num"));
			int number2=Integer.parseInt(request.getParameter("num2"));
           if(number>0 ){
           for(i=0;i<number;i++)
                {
	         sql=sql+   "  c_id ='" + "A0000" + "' "; 
                 sql=sql+   "  or  "; 
                }
           }
           if(number2>0 ){
           for(i=0;i<number2;i++)
                {
	         sql=sql+   "  c_id ='" + "A0001" + "' "; 
                 sql=sql+   "  or  "; 
                }
           }
	  } 

	%> 
    </body>
</html>

謝謝大家

yi741963 iT邦新手 5 級 ‧ 2019-03-22 11:24:03 檢舉
之前有客戶也有這個需求,
是調程式, 重復呼叫SQL, 不調SQL
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
居然解出來了
iT邦好手 1 級 ‧ 2019-03-20 11:05:23
最佳解答

我剛剛看了你的程式...
SQL你使用OR當然只有各一次
將 OR 改成 UNION ALL 試試看

或者byte[]陣列相加看看
應該就可以出來

看更多先前的回應...收起先前的回應...
joker iT邦新手 5 級 ‧ 2019-03-20 11:23:13 檢舉

你好:
我查了一下UNION ALL她似乎是要sql資料有重複她才會印出來!?
因為我的客戶資料都沒重複,好像就不行....

重sql想要輸出相同筆數多重資料的做法已經是不對了。
不要再往這方面去想了。去換個做法吧。

如取出資料後再來複製份數。

若真不行那用陣列相加試試看吧(?

其實 union all 反而是不管有沒重複都會顯示結果
只是你這段要重複:
select* from send where

舉個例子:
select* from send where c_id ='A0000'
union all
select* from send where c_id ='A0000'

joker iT邦新手 5 級 ‧ 2019-03-20 14:08:34 檢舉

partyyaya 謝謝

0

有點,難了解你的問題點在哪。

這只是換一下想法的問題。
只是取出資料後 * 幾次做輸出?

這從sql要取多重相同資料,是什麼觀念啊??
難不成你認為 or 三個同樣的id,就會出現三筆資料嗎??

正常你因該是先取得資料後。再從資料去copy幾份出來才對。怎麼會認為可以從sql下手啊?

1
海綿寶寶
iT邦大神 1 級 ‧ 2019-03-20 11:58:03

如果可以「把張數記錄在資料表裡」
以下是可能的解法
https://ithelp.ithome.com.tw/upload/images/20190320/200017870FjdiI2vG5.png

create table customer (
  cust_id varchar(5),
  cust_name varchar(10),
  cust_count int
);

insert into customer (cust_id, cust_name, cust_count) values
  ('A0000', 'A', 2),
  ('A0001', 'B', 3),
  ('A0002', 'C', 5)
;

SELECT c.cust_name
FROM customer c
    JOIN (
                SELECT 1 as num UNION
                SELECT 2 UNION
                SELECT 3 UNION
                SELECT 4 UNION
                SELECT 5 UNION
                SELECT 6
               ) n
    ON c.cust_count >= n.num
WHERE cust_id='A0000' OR cust_id='A0001'
ORDER BY cust_name;

我要發表回答

立即登入回答