Php 我无法使用mysql从具有内部联接的数据库中选择数据
我无法从数据库中选择数据 我的表格结构如下所示 客户表Php 我无法使用mysql从具有内部联接的数据库中选择数据,php,mysql,Php,Mysql,我无法从数据库中选择数据 我的表格结构如下所示 客户表 id name 10 geetha 客户国家表 id cust_id country 1 10 6 2 10 16 我是这样得到结果的 customer name country geetha 6 geetha 16 但我只想获得一个客户的数据一次,即不重复 customer name co
id name
10 geetha
客户国家表
id cust_id country
1 10 6
2 10 16
我是这样得到结果的
customer name country
geetha 6
geetha 16
但我只想获得一个客户的数据一次,即不重复
customer name country
geetha 6
我的问题是
SELECT customer.name,customer.id,customer_country.country_id, customer_country.cust_id
FROM customer
INNER JOIN customer_country on customer.id = customer_country.cust_id
如果在
customer\u country
表中有重复项,则需要选择其中一项。下面是一个使用max()
的方法:
如果要将所有这些内容都列在一个列表中,请使用group\u concat()
:
仅适用于第一条记录,适用于结尾:限制1
SELECT customer.name,customer.id,customer_country.country_id,
customer_country.cust_id from customer
inner join customer_country on customer.id= customer_country.cust_id limit 1
试试这个,我在customer\u country.cust\u id之前添加了distinct
SELECT customer.name,customer.id,customer_country.country_id, distinct customer_country.cust_id
FROM customer
INNER JOIN customer_country on customer.id = customer_country.cust_id
您想要第一个还是最后一个插入的国家id?
SELECT customer.name,customer.id,customer_country.country_id,
customer_country.cust_id from customer
inner join customer_country on customer.id= customer_country.cust_id limit 1
SELECT customer.name,customer.id,customer_country.country_id, distinct customer_country.cust_id
FROM customer
INNER JOIN customer_country on customer.id = customer_country.cust_id