dongpiao8821 2017-09-03 16:52
浏览 30
已采纳

在php中分组函数的结果

Hi there I have a query result that gives me 2 elements starting the same but ending diferently, this way:

  • btdeucat_de
  • btdeucat_ca

By means of a php function I can eliminate the difference, but the echo still gives me 2 elements, how can I group them to form just one element:

  • btdeucat

(I have tried GROUP BY at the query but that does not help because at the db names are different)

Here's the code I have:

<?php
$result0 = mysqli_query($dbiac, "SELECT * FROM corpus_info WHERE corpus IN (SELECT corpus FROM corpus_alignments)") or die(mysqli_error($dbiac));
while($cpsblg = mysqli_fetch_array($result0)){

// eliminate the difference and echo it to the page
$cpsblg = preg_replace('"_(de|en|ca|es|fr|it|pt)$"', '', $cpsblg['corpus']);
echo $cpsblg."<br />";
?>

Results in:

  • btdeucat
  • btdeucat
  • 写回答

1条回答 默认 最新

  • douhai5835 2017-09-04 04:14
    关注

    Can't you just do this?

    SELECT c.corpus_initial
    FROM (SELECT SUBSTRING_INDEX(corpus, '_', 1) AS corpus_initial
          FROM corpus_info 
          WHERE corpus IN (SELECT corpus FROM corpus_alignments)) c
    GROUP BY c.corpus_initial
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟