我希望按日期顺序获取数据。我正在使用工会。
我已经浏览了下面的网址。但我没有得到明确的解决方案。
mysql order by with union doesn't seem to work
我的查询
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JPanel
{
List<String[]> editorData = new ArrayList<String[]>(3);
public TableComboBoxByRow()
{
setLayout( new BorderLayout() );
// Create the editorData to be used for each row
editorData.add( new String[]{ "Red", "Blue", "Green" } );
editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
editorData.add( new String[]{ "Apple", "Orange", "Banana" } );
// Create the table with default data
Object[][] data =
{
{"Color", "Red"},
{"Shape", "Square"},
{"Fruit", "Banana"},
{"Plain", "Text"}
};
String[] columnNames = {"Type","Value"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 1 && row < 3)
{
JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
return new DefaultCellEditor( comboBox1 );
}
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
// table.getColumnModel().getColumn(1).setCellRenderer(new ComboBoxRenderer2() );
}
/*
class ComboBoxRenderer2 extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
label.setIcon(UIManager.getIcon("Table.descendingSortIcon"));
return label;
}
}
*/
private static void createAndShowUI()
{
JFrame frame = new JFrame("Table Combo Box by Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableComboBoxByRow() );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
我的结果
(SELECT n.nid,
Max(na.gid) AS mid,
fav.field_date_posted_value AS pdate
FROM `node` AS n
JOIN nodeaccess AS na
ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav
ON fav.entity_id = n.nid
WHERE ( na.gid IN( 10, 11 )
AND ( n.status = '1' )
AND ( n.type IN ( 'article', 'blog', 'events', 'media',
'press_releases', 'expert_speak', 'feature',
'case_study',
'news', 'the_igtb_series', 'trend', 'white_paper' )
) )
GROUP BY n.nid
ORDER BY pdate DESC)
UNION
(SELECT n.nid,
Max(na.gid) AS mid,
fav.field_date_posted_value AS pdate
FROM `node` AS n
JOIN nodeaccess AS na
ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav
ON fav.entity_id = n.nid
WHERE ( na.gid IN( 2 )
AND ( n.status = '1' )
AND ( n.type IN ( 'article', 'blog', 'events', 'media',
'press_releases', 'expert_speak', 'feature',
'case_study',
'news', 'the_igtb_series', 'trend', 'white_paper' )
) )
GROUP BY n.nid
ORDER BY pdate DESC)
LIMIT
10
在上面的网址中,日期顺序无效。我不想像普通人那样订购。对于两个选择查询,我需要单独的order by(在UNION中使用)。
更新2:
+-------+------+---------------------+
| nid | mid | pdate |
+-------+------+---------------------+
| 12472 | 10 | 2015-05-11 00:00:00 |
| 12473 | 10 | 2015-04-03 00:00:00 |
| 12475 | 10 | 2015-06-08 00:00:00 |
| 12476 | 10 | 2015-12-15 01:55:48 |
| 12477 | 10 | 2014-06-30 00:00:00 |
| 12478 | 10 | 2013-12-26 00:00:00 |
| 12482 | 10 | 2014-02-02 00:00:00 |
| 12483 | 10 | 2014-09-01 00:00:00 |
| 12484 | 10 | 2015-12-04 00:00:00 |
| 12485 | 10 | 2015-08-14 00:00:00 |
+-------+------+---------------------+
以上给出了以下结果。
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY pdate DESC)
UNION
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY pdate DESC)
ORDER BY pdate DESC LIMIT 10
在此结果中,我不想重新排列“ mid ”
列答案 0 :(得分:1)
您需要包含两个select语句的union的结果,并按pdate
排序。
如下:
SELECT * from
(
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY pdate DESC)
UNION
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY pdate DESC)
) as UnionTable
ORDER BY pdate DESC limit 10
答案 1 :(得分:1)
我不确定为什么甚至需要union
。并且,我不完全理解第三列值应该是什么,因为它不是聚合函数的参数而不在group by
中。
但是,我会将查询编写为:
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate
FROM `node` n JOIN
nodeaccess na
ON na.nid = n.nid LEFT JOIN
field_data_field_date_posted fav
ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY pdate DESC
LIMIT 10
) UNION ALL
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate
FROM `node` n JOIN
nodeaccess na
ON na.nid = n.nid LEFT JOIN
field_data_field_date_posted fav
ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY pdate DESC
LIMIT 10
)
ORDER BY pdate DESC
LIMIT 10;
注意:
ORDER BY
订购所有结果。UNION ALL
代替UNION
,这样您的查询就不会产生删除重复项的开销。答案 2 :(得分:1)
创建一个附加列,以便您可以订购每个UNION
查询
<强> Sql Fiddle Demo 强>
SELECT '1' as grp, 1 as ID
UNION
SELECT '1' as grp, 3 as ID
UNION
SELECT '1' as grp, 2 as ID
UNION
SELECT '1' as grp, 5 as ID
UNION
SELECT '2' as grp, 4 as ID
UNION
SELECT '2' as grp, 7 as ID
UNION
SELECT '2' as grp, 2 as ID
UNION
SELECT '2' as grp, 5 as ID
ORDER BY grp, ID
<强>输出强>
答案 3 :(得分:1)
可以删除另外两个ORDER BY子句。实际上很少有DB只能在Union语句之前放置ORDER BY OR LIMIT子句。您可能想尝试以下内容:
(
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
)
UNION
(SELECT n.nid, max(na.gid) as mid, fav.field_date_posted_value as pdate FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid)
) order by pdate DESC limit 10
答案 4 :(得分:1)
试试这个
SELECT n.nid, max(na.gid) as mid,fav.field_date_posted_value, UNIX_TIMESTAMP(fav.field_date_posted_value) as pdate, 1 as ob FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(10,11) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
UNION ALL
SELECT n.nid, max(na.gid) as mid,fav.field_date_posted_value, UNIX_TIMESTAMP(fav.field_date_posted_value) as pdate, 2 as ob FROM `node` as n
JOIN nodeaccess AS na ON na.nid = n.nid
LEFT JOIN field_data_field_date_posted AS fav ON fav.entity_id = n.nid
WHERE (na.gid IN(2) AND (n.status = '1') AND (n.type IN ('article','blog', 'events', 'media', 'press_releases', 'expert_speak', 'feature', 'case_study', 'news', 'the_igtb_series', 'trend', 'white_paper')) )
GROUP BY n.nid
ORDER BY ob ASC, pdate DESC
修正了按日期排序的基数或组
答案 5 :(得分:1)
我使用临时表解决联合上的订购问题:
CREATE TEMPORARY TABLE IF NOT EXISTS temp_table1 AS
(
SELECT *
FROM table1
ORDER BY < your specific order > )
CREATE TEMPORARY TABLE IF NOT EXISTS temp_table2 AS
(
SELECT *
FROM table2
ORDER BY < your specific order > )
SELECT * FROM temp_table1
UNION ALL
SELECT * FROM temp_table2