Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请教sql查询2张表的问题

请教sql查询2张表的问题

Jun 23, 2016 pm 02:04 PM

请教sql查询2张表的问题:
A表为
id title picid
其中picid存储信息为
a:4:{s:6:"attach";s:2:"74";s:9:"show_type";s:1:"0";s:9:"user_bind";s:1:"1";s:10:"topic_bind";s:0:"";}

B表为
p_id purl
我首先通过查询A表获得了picid字段中的attach数据,输出为数组形式为
aryy{
【0】74
【1】88
....
}
那么我如何通过查询A表得到的attach去查询B表 p_id 为74 88的匹配id呢?
能给个代码解释下吗?


回复讨论(解决方案)

设第一步得到的数组为
$ar = array(74, 88);
则有
$sql = 'select * from B表 where p_id in (' . join(',', $ar) . ')';

如果 picid 中 attach 出现的次序是固定的话,还可以直接关联两表查询

比如
select p_id, purl from B表, A表 where p_id = SUBSTRING_INDEX(SUBSTRING_INDEX(picid, 4), '"', -1)

设第一步得到的数组为
$ar = array(74, 88);
则有
$sql = 'select * from B表 where p_id in (' . join(',', $ar) . ')';

如果 picid 中 attach 出现的次序是固定的话,还可以直接关联两表查询

如果出现次序不是固定的呢?能关联两表查询吗

用 LOCATE 函数先定位,应该也是可以的

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles