简体   繁体   中英

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. 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:

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:

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

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. 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 . Do you actually have data where id = 4 or topid = 4 in the system?

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.


I have a field called 'id' in both tables. How can I control which one I'm accessing with PHP after I read data into the array with $row = @mysql_fetch_assoc($result) ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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