简体   繁体   English

协助MySQL JOIN

[英]Assistance with MySQL JOIN

I have two tables, one with categories and subcategories. 我有两个表,一个有类别和子类别。 Each category and subcategory has an id and if it's a subcategory, it's got a topid != 0 referring what it's a subcategory of. 每个类别和子类别都有一个id,如果它是一个子类别,它有一个topid != 0引用它的子类别。 The other table "markers" has a field 'cat' which correlates with the category field 'name' Now I want to select everything from markers with category.id = 4 OR category.topid = 4 so I tried this query: 其他表“markers”有一个字段'cat',它与类别字段'name'相关联现在我想从带有category.id = 4 OR category.topid = 4标记中选择所有内容,所以我尝试了这个查询:

SELECT * FROM `xymply_markers`
JOIN `xymply_categories`
    ON xymply_markers.cat = xymply_categories.name
WHERE xymply_categories.topid=4
    OR xymply_categories.id=4

Which doesn't return me anything even tho I do have such elements in my table "markers". 即使我在表格“标记”中有这样的元素,这也不会给我任何回报。 Any assistance would be appreciated! 任何援助将不胜感激!

Table schemas: 表模式:

`xymply_categories` (
  `id` int(11) NOT NULL auto_increment,
  `topid` int(11) NOT NULL,
  `name` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;

`xymply_markers` (
  `created` date NOT NULL,
  `id` int(11) NOT NULL auto_increment,
  `sdate` date NOT NULL,
  `hdate` date NOT NULL,
  `name` varchar(60) NOT NULL,
  `address` varchar(80) NOT NULL,
  `unit` varchar(6) NOT NULL,
  `lat` decimal(10,7) NOT NULL,
  `lng` decimal(10,7) NOT NULL,
  `type` varchar(30) NOT NULL,
  `userid` int(11) NOT NULL,
  `adtext` text NOT NULL,
  `phone` varchar(20) NOT NULL,
  `email` varchar(30) NOT NULL,
  `url` varchar(30) NOT NULL,
  `cat` varchar(4) NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=151 DEFAULT CHARSET=utf8 AUTO_INCREMENT=151 ;

Sample Data: 样本数据:

xymply_categories: xymply_categories:

id  1
topid 0
name 'vehicle'
--------------
id  2
topid 1
name 'bike'
--------------
id  3
topid 1
name 'truck'

xymply_markers: xymply_markers:

id 1
sdate 2012-03-01
hdate 2012-04-01
name 'TEST'
address '1234 TEST'
unit''
lat 49.0
lng -123.0
adtext 'TEST'
phone '1234567890'
email 'email@email.com'
url 'www.url.com'
cat 'bike'

--------------

id 1
sdate 2012-03-01
hdate 2012-04-01
name 'TEST'
address '1234 TEST'
unit''
lat 49.5
lng -123.5
adtext 'TEST'
phone '1234567890'
email 'email@email.com'
url 'www.url.com'
cat 'vehicle'

One problem is that the xymply_markers.cat field is VARCHAR(4) but the xymply_categories.name field is TEXT, and contains values longer than 4 characters. 一个问题是xymply_markers.cat字段是VARCHAR(4)但xymply_categories.name字段是TEXT,并且包含长度超过4个字符的值。 Either you're not giving us the accurate schema, or you're confused about which columns join, or you're never going to see any trucks or vehicles. 您要么没有给我们准确的架构,要么您对哪些列加入感到困惑,或者您永远不会看到任何卡车或车辆。 Columns which join should have the same type almost without exception (I've never seen a good reason for an exception). 连接的列应该具有相同的类型,几乎没有例外(我从未见过一个例外的好理由)。

You are then asking about id = 4 or topid = 4 , but the sample data you show only has id = 1 or topid = 1 . 然后,您询问的是id = 4topid = 4 ,但您显示的样本数据仅具有id = 1topid = 1 Do you actually have data where id = 4 or topid = 4 in the system? 您实际上在系统中是否有id = 4topid = 4的数据?

Between these two lots of confusion, it is hard to know what we're up against. 在这两个混乱之间,很难知道我们面临的是什么。 If you have data that joins and has the relevant topid or id values, then your query should work. 如果您有联接的数据并具有相关的topidid值,则您的查询应该可以使用。


I have a field called 'id' in both tables. 我在两个表中都有一个名为'id'的字段。 How can I control which one I'm accessing with PHP after I read data into the array with $row = @mysql_fetch_assoc($result) ? 在使用$row = @mysql_fetch_assoc($result)数据读入数组后,如何控制使用PHP访问哪一个?

The simplest way is to ensure that each result column has a unique name, creating one with an 'alias', as in: 最简单的方法是确保每个结果列都有一个唯一的名称,创建一个带有“别名”的名称,如下所示:

SELECT c.id AS category_id,
       c.topid,
       c.name AS category_name,
       m.id AS marker_id,
       m.name AS marker_name,
       ...

PHP will associate the alias names with the the data in the row. PHP会将别名与行中的数据相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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