简体   繁体   English

我无法使用mysql通过内部联接从数据库中选择数据

[英]I cant select data from database with inner join using mysql

I cant select data from database . 我无法从数据库中选择数据。

My table structrure is given below 我的表结构如下

customer table 客户表

id      name
10      geetha

customer country table 客户国家表

id    cust_id  country

1       10      6
2       10      16

I got the result like these way 我这样得到结果

customer name      country


geetha             6
geetha             16

But i want to get the one customer data only one time ie with out repeating. 但是我只想一次获得一个客户数据,即无需重复。

customer name      country


geetha             6

my query is 我的查询是

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

If you have duplicates in the customer_country table, then you need to choose one of them. 如果customer_country表中有重复项,则需要选择其中之一。 Here is one method using max() : 这是使用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;

If you want all of them in a list, use group_concat() : 如果要将它们全部都放在列表中,请使用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;

For first record only, apply to the end: limit 1 仅适用于第一笔记录,适用于结尾:限制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

try this i add distinct before customer_country.cust_id 试试这个我在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM