我尝试合并两张桌子。示例,表 A 和 B。表 A 有 2 列(id 和 value)表 A 有 3 行 (1, a)、(1, b) 和 (1, c)。表 B 有 1 列和 1 行(id)为(1)我尝试使用join table执行此操作,结果:3 行 id 和值为 (1, a), (1, b), (1, c)这不是我想要的,我需要创建如下:结果:只有 1 行,id 为 (1),value 为 (a, b, c)但是,我只想从“value”中获取“c”数据。那么,我想要的结果是(1, c),怎么样?编辑:检查,有工作:http://sqlfiddle.com/#! 9/b0fd3b/2它不适用于我的代码,未找到结果。检查一下:<?phpif (isset($_GET['page_no']) && $_GET['page_no']!="") { $page_no = $_GET['page_no'];}else { $page_no = 1;}$total_records_per_page = 3;$offset = ($page_no-1) * $total_records_per_page;$previous_page = $page_no - 1;$next_page = $page_no + 1;$adjacents = "2"; $result_count = mysqli_query($conn, "SELECT COUNT(*) As total_records FROM b");$total_records = mysqli_fetch_array($result_count);$total_records = $total_records['total_records']; $total_no_of_pages = ceil($total_records / $total_records_per_page);$second_last = $total_no_of_pages - 1; // total page minus 1$result = mysqli_query($conn, "SELECT id, a.meta_key, a.meta_value FROM a JOIN b USING (id) WHERE a.meta_value = 'value2' LIMIT $offset, $total_records_per_page");if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result)) { echo $row['id']; echo $row['key']; echo $row['value']; }}else { echo "no found";}?>// pagination....
3 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
尝试下面的查询。我希望它能得到你想要的结果。
SELECT a.id, a.value,
FROM a
INNER JOIN b ON a.id=b.id;
ORDER BY
a.id DESC;
侃侃无极
TA贡献2051条经验 获得超10个赞
只有 1 行,id 为 (1),value 为 (a, b, c)
SELECT id, GROUP_CONCAT(value ORDER BY value) values
FROM a
JOIN b USING (id)
GROUP BY id
但是,我只想从“value”中获取“c”数据。
SELECT id, a.value
FROM a
JOIN b USING (id)
WHERE a.value = 'c'
慕桂英3389331
TA贡献2036条经验 获得超8个赞
你可以这样做:
select a.*
from a join
b
on a.id = b.id
order by a.value desc
limit 1;
我对为什么join需要 a 有点困惑:
select a.*
from a
order by value desc
limit 1;
但你似乎想要join.
- 3 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消