[英]I cant select data from database with inner join using mysql
我无法从数据库中选择数据。
我的表结构如下
客户表
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_country
表中有重复项,则需要选择其中之一。 这是使用max()
一种方法:
select c.name, max(cc.country_id)
from customer c inner join
customer_country cc
on c.id = cc.cust_id
group by c.name;
如果要将它们全部都放在列表中,请使用group_concat()
:
select c.name, group_concat(cc.country_id) as countries
from customer c inner join
customer_country cc
on c.id = cc.cust_id
group by c.name;
仅适用于第一笔记录,适用于结尾:限制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_country.cust_id之前添加独特
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.