0

我有一个包含不同字段的表,但我想显示两个字段的结果,其中一个包含重复值。考虑:

  F1  |   F2
-----------------
m1    | manchester
m2    | manchester 
cm3   | london
k4    | birmingham

我想要这样的结果,如果我按下m

m1
m2
cm3
manchester
birmingham

我是 php 和 mysql 的新手

4

1 回答 1

1

You can use a union query to get what you want like this:

select
    F1 as m
from 
    tableName
where
    F1 like '%m%'
union all
select
    F2 as m
from 
    tableName
where
    F2 like '%m%'

Edit: This will automatically remove duplicates as shown below:

mysql> select * from first;
+------+-------+
| id   | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    6 | ffff  |
+------+-------+
5 rows in set (0.00 sec)

mysql> select * from first union select * from first;
+------+-------+
| id   | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    6 | ffff  |
+------+-------+
5 rows in set (0.00 sec)
于 2012-09-05T13:51:02.227 回答