hive>select concat('a','b')
ab
hive>select concat('a','肆没仔b',null)
NULL
MYSQL里的CONCAT函数用于将两个字符串连接起来,形成一个单一的字符串。
如下面的例子:
mysql> select concat('11','22','33')
+------------------------+
| concat('11','22','33') |
+------------------------+
| 112233 |
+------------------------+
1 row in set (0.00 sec)
MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
mysql> select concat('11','22',null)
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
MySQL中concat_ws函数
使用方法:
contcat_ws(separator,str1,str2,...)
contcat_ws() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。老历
注意:
如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值侍档搜。
如连接后以逗号分隔
mysql> select concat_ws(',','11','22','33')
+-------------------------------+
| concat_ws(',','11','22','33') |
+-------------------------------+
| 11,22,33 |
+-------------------------------+
1 row in set (0.00 sec)
和MySQL中concat函数不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
mysql> select concat_ws(',','11','22',NULL)
+-------------------------------+
| concat_ws(',','11','22',NULL) |
+-------------------------------+
| 11,22 |
+-------------------------------+
1 row in set (0.00 sec)
HQL里的CONCAT函数大致用法更SQL的相同
我使用的常用形式为:
select a, b, concat_ws(',' , collect_set(cast(c as string)))
from table group by a,b
上文HQL中collect_set 有两个作用,第一个是 去重 ,去除group by后的重复元素,
第二个是形成一个 集合 ,将group by后属于同一组的第三列集合起来成为一个集合。与contact_ws
结合使用就是将这些元蠢扮素以逗号分隔形成字符串。当使用collect_list是则不会去重,它会将第三列的集合全部都列出来
cast ,用法cast(value as type),将某个列的值显示的转化为某个类型,cast(age as string ) 将int类型的数据转化为了String类型。
import javax.swing.*import java.awt.*
import java.awt.event.*
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled// 帮助运算的布尔笑肆变量
private double storedNumber// 目前的结果
private char lastOperator// 表示上一运算符
private JTextField operation// 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear// 运算符
private JButton[] numbers// 数字
// 构造者
public Calculator()
{
setTitle("Calculator")
// 初始化变量
dotExist = false// 表示当前的数是否有小数点
operated = false// 表示任意运碰埋轿算符是否被按下
equaled = false// 表示等号是否被按下
storedNumber = 0
lastOperator = '?'液巧
// 初始化窗口变量
operation = new JTextField("0")
operation.setEditable(false)
numbers = new JButton[10]
for (int i = 0i <10i++)
numbers[i] = new JButton("" + i)
dot = new JButton(".")
plus = new JButton("+")
minus = new JButton("-")
multi = new JButton("*")
div = new JButton("/")
sqrt = new JButton("√")
equal = new JButton("=")
changePN = new JButton("±")
clear = new JButton("AC")
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout()
getContentPane().setLayout(layout)
addComponent(layout, operation, 0, 0, 4, 1)
addComponent(layout, numbers[1], 1, 0, 1, 1)
addComponent(layout, numbers[2], 1, 1, 1, 1)
addComponent(layout, numbers[3], 1, 2, 1, 1)
addComponent(layout, numbers[4], 2, 0, 1, 1)
addComponent(layout, numbers[5], 2, 1, 1, 1)
addComponent(layout, numbers[6], 2, 2, 1, 1)
addComponent(layout, numbers[7], 3, 0, 1, 1)
addComponent(layout, numbers[8], 3, 1, 1, 1)
addComponent(layout, numbers[9], 3, 2, 1, 1)
addComponent(layout, dot, 4, 0, 1, 1)
addComponent(layout, numbers[0], 4, 1, 1, 1)
addComponent(layout, sqrt, 4, 2, 1, 1)
addComponent(layout, plus, 1, 3, 1, 1)
addComponent(layout, minus, 2, 3, 1, 1)
addComponent(layout, multi, 3, 3, 1, 1)
addComponent(layout, div, 4, 3, 1, 1)
addComponent(layout, equal, 5, 0, 2, 1)
addComponent(layout, changePN, 5, 2, 1, 1)
addComponent(layout, clear, 5, 3, 1, 1)
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource()
if (btn == clear)
{
operation.setText("0")
dotExist = false
storedNumber = 0
lastOperator = '?'
}
else if (btn == equal)
{
operate('=')
equaled = true
}
else if (btn == plus)
{
operate('+')
equaled = false
}
else if (btn == minus)
{
operate('-')
equaled = false
}
else if (btn == multi)
{
operate('*')
equaled = false
}
else if (btn == div)
{
operate('/')
equaled = false
}
else if (btn == changePN)
{
operate('p')
operate('=')
equaled = true
}
else if (btn == sqrt)
{
operate('s')
operate('=')
equaled = true
}
else
{
if (equaled)
storedNumber = 0
for (int i = 0i <10i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i)
else if(! operated)
operation.setText(operation.getText() + i)
else
{
operation.setText("" + i)
operated = false
}
}
if (btn == dot &&! dotExist)
{
operation.setText(operation.getText() + ".")
dotExist = true
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue()
if (lastOperator == '?')
storedNumber = currentNumber
else if (lastOperator == '+')
storedNumber += currentNumber
else if (lastOperator == '-')
storedNumber -= currentNumber
else if (lastOperator == '*')
storedNumber *= currentNumber
else if (lastOperator == '/')
storedNumber /= currentNumber
else if (lastOperator == 'p')
storedNumber *= -1
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber)
else if (lastOperator == '=' &&equaled)
storedNumber = currentNumber
operation.setText("" + storedNumber)
operated = true
lastOperator = operator
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints()
constraints.fill = GridBagConstraints.BOTH
constraints.insets = new Insets(10, 2, 10, 2)
constraints.weightx = 100
constraints.weighty = 100
constraints.gridx = col
constraints.gridy = row
constraints.gridwidth = width
constraints.gridheight = height
layout.setConstraints(component, constraints)
if (component instanceof JButton)
((JButton)component).addActionListener(this)
getContentPane().add(component)
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator()
calc.setSize(290, 400)
calc.setVisible(true)
}
}
如果你想加sin cos tan的话就建它们的按钮 在actionPerformed方法中的if-else语句中加else if (btn == sin){operate('S')operate('=')equaled = true}然后在operate方法加对应的语句就行了.记忆M+, M-没时间搞 这个应该比较简单容易明白吧.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)