开发者

duplicate column names in query

I don't think it's possible but I thought I would ask anyways.

I have a MySql 5.+ query as such...

SELECT p.*, u.* FROM princess p JOIN unicorn u ON p.id = u.princess_id

It's kind enough to return me all of the princesses and unicorns ;). The problem is that the ret开发者_开发技巧urned result set may contain duplicate column names (not good). If both tables have a column named name how can I differentiate it without explicitly coding the alias like p.name as 'princess_name' and u.name as 'unicorn_name'?

Also, I can't use the column index so if there isn't a better way I'll just hand code all the aliases myself.


SELECT * is bad idea for several reasons, one of which you've just found. Use an explicit column list and create your own aliases.


You'll have to hand code all the aliases yourself. ;) Anyway, getting rid of those * won't be a bad thing.


It is not possible, and also, try to avoid using the * in SQL. It is a bad habit. Writting all your fields is better for readibility AND PERFORMANCE, don't forget that!


You will have to hand code the aliases. And this is good. It is always a good idea to explicitly mention all the column names.


If you fetch the rows as an array, you could rely on the order of the data. If you fetch them as a map/dictionary...you're probably out of luck. Really though, aliasing is a much better idea.

PS. Star selects are bad form.


String query1="SHOW COLUMNS FROM uploadfile LIKE '"+label+"'"; ResultSet rs=st.executeQuery(query1);

                          while(rs.next())
                          {
                         //   int rs = 0;

                              pw.println("<script>alert(' throw new DuplicateName()')</script>"); 

                              pw.write("Error in file insertion"+rs);
                              response.sendRedirect("AddAttribute.jsp?status='Error'");

                          }

                              String query ="ALTER TABLE uploadfile ADD  "+label+" varchar (10)";
                               System.out.println("welcome in java ");

                              int rs1=st.executeUpdate(query);


                               response.sendRedirect("afterlabel_updatefile.jsp?status='success'");

                           //  response.sendRedirect("AdminHome.jsp"); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜