MySQL UNION正在改变数据格式

时间:2015-10-02 21:02:25

标签: mysql union

我对此感到有些困惑。我有一些不同公司的类似表,试图联合它们以获得组合数据。但是,UNION更改了我的一个列的数据格式,这会破坏使用该列的PHP代码。

每个表中的列是:

branch tinyint(2) unsigned zerofill

以下是从第一家公司选择的情况(这只是一个例子,我正在做的实际选择更加复杂):

SELECT DISTINCT branch FROM company1
+--------+
| branch |
+--------+
|     01 |
|     02 |
|     03 |
|     04 |
|     40 |
|     90 |
+--------+

第二家公司:

SELECT DISTINCT branch FROM company2
+--------+
| branch |
+--------+
|     01 |
|     02 |
|     03 |
|     04 |
|     05 |
|     40 |
|     90 |
+--------+

最后是UNION:

SELECT DISTINCT branch FROM company1 UNION SELECT DISTINCT branch FROM company2 

+--------+
| branch |
+--------+
|      1 |
|      2 |
|      3 |
|      4 |
|     40 |
|     90 |
|      5 |
+--------+

你可以看到我失去了UNION的领先0。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

来自MySQL documentation

  

当表达式或UNION查询中涉及列时,将忽略ZEROFILL属性。

在查询中明确使用public String confirmOrder() throws IllegalStateTransitionException { shoppingcart.setUser(userholder.getCurrentUser()); shoppingcart.setCompleted(OrderStatus.COMPLETED); shoppingcart.setDate(new Date()); // Setting up sessions Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); session.save(shoppingcart); // This Line is causing the Exception, I checked by debugging session.close(); updateStocks(); FacesContext fc = FacesContext.getCurrentInstance(); fc.getELContext().getELResolver().setValue(fc.getELContext(), null, "shoppingCart", null); //printing Invoice //writing to file try { String content = "Customer Name: " + shoppingcart.getUser().firstName + " " + shoppingcart.getUser().lastName +"\r\n" + "Address : " + shippingAddress + "\r\n" + "Date Received: " + shoppingcart.getDate() + "\r\n" + "Order Items :" + shoppingcart.getOrders() + "\r\n" + "Money to be received :" + productHolder.subTotal(); // File file = new File("/Users/Vinod/Documents/newfile.txt"); File file = new File("F:/" + shoppingcart.getId() + shoppingcart.getUser().firstName + "invoice.txt"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); }catch (IOException e) { e.printStackTrace(); } return "orderComplete"; }

LPAD