SQL:从两个表中选择行的第一个字母

时间:2022-08-11 15:40:58

i need to get queries of two table from its first letters.

我需要从第一个字母中获取两个表的查询。

from searching , i got the following query :

从搜索,我得到以下查询:

SELECT substr( latin_name, 1, 1 ) AS alpha
FROM singers
GROUP BY substr( latin_name, 1, 1 )
LIMIT 0 , 30

and now i need to mix it with another table named groups

现在我需要将它与另一个名为groups的表混合

how can i collect all first letters of two different tables by one query?

如何通过一个查询收集两个不同表的所有首字母?

2 个解决方案

#1


1  

Use UNION syntax: http://dev.mysql.com/doc/refman/5.0/en/union.html

使用UNION语法:http://dev.mysql.com/doc/refman/5.0/en/union.html

This is how I would do it:

我就是这样做的:

SELECT LOWER(LEFT(`latin_name`, 1))
FROM `singers`
UNION
SELECT LOWER(LEFT(`latin_name`, 1))
FROM `groups`
ORDER BY 1 ASC

#2


1  

Use a UNION Statement therefore:

因此使用UNION声明:

SELECT ... FROM table1
UNION 
SELECT ... FROM table2

#1


1  

Use UNION syntax: http://dev.mysql.com/doc/refman/5.0/en/union.html

使用UNION语法:http://dev.mysql.com/doc/refman/5.0/en/union.html

This is how I would do it:

我就是这样做的:

SELECT LOWER(LEFT(`latin_name`, 1))
FROM `singers`
UNION
SELECT LOWER(LEFT(`latin_name`, 1))
FROM `groups`
ORDER BY 1 ASC

#2


1  

Use a UNION Statement therefore:

因此使用UNION声明:

SELECT ... FROM table1
UNION 
SELECT ... FROM table2