简体   繁体   中英

need help Mysql query

I am trying to achieve displaying a field in my database.

basically at the moment i have

'SELECT
 historylist.artist,
 historylist.ID,
 artistlist.lyrics,
 artistlist.ID
FROM historylist
 INNER JOIN artistlist
  ON historylist.ID = artistlist.ID
 ORDER BY historylist.date_played DESC
LIMIT 1;'     

Now this is not correct. I need to use the history list ID to link the artistlist ID. Then grab the field artistlist.lyrics. then display it. Right now when i do it like that it shows the lyrics field but its null. So i am guessing its searching historylist table

  CREATE TABLE `historylist` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `songID` int(11) NOT NULL DEFAULT '0',
   `filename` varchar(255) NOT NULL DEFAULT '',
   `date_played` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
   `duration` int(11) NOT NULL DEFAULT '0',
   `artist` varchar(255) NOT NULL DEFAULT '',
   `title` varchar(255) NOT NULL DEFAULT '',
   `album` varchar(255) NOT NULL DEFAULT '',
   `albumyear` varchar(4) NOT NULL DEFAULT '',
   `website` varchar(255) NOT NULL DEFAULT '',
   `buycd` varchar(255) NOT NULL DEFAULT '',
   `picture` varchar(255) NOT NULL DEFAULT '',
   `listeners` mediumint(9) NOT NULL DEFAULT '0',
   `label` varchar(100) NOT NULL DEFAULT '',
   `pline` varchar(50) NOT NULL DEFAULT '',
   `trackno` int(11) NOT NULL DEFAULT '0',
   `composer` varchar(100) NOT NULL DEFAULT '',
   `ISRC` varchar(50) NOT NULL DEFAULT '',
   `catalog` varchar(50) NOT NULL DEFAULT '',
   `UPC` varchar(50) NOT NULL DEFAULT '',
   `feeagency` varchar(20) NOT NULL DEFAULT '',

   PRIMARY KEY (`ID`),
   KEY `date_played` (`date_played`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

that code above is the historylist table

this one is the

  CREATE TABLE IF NOT EXISTS `artistlist` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(255) NOT NULL DEFAULT '',
  `diskID` int(11) NOT NULL DEFAULT '0',
  `flags` varchar(10) NOT NULL DEFAULT 'NNNNNNNNNN',
  `songtype` char(1) NOT NULL DEFAULT 'S',
  `status` tinyint(4) NOT NULL DEFAULT '0',
  `weight` double NOT NULL DEFAULT '50',
  `balance` double NOT NULL DEFAULT '0',
  `date_added` datetime DEFAULT NULL,
  `date_played` datetime DEFAULT NULL,
  `date_artist_played` datetime DEFAULT '2002-01-01 00:00:01',
  `date_album_played` datetime DEFAULT '2002-01-01 00:00:01',
  `date_title_played` datetime DEFAULT '2002-01-01 00:00:01',
  `duration` int(11) NOT NULL DEFAULT '0',
  `artist` varchar(255) NOT NULL DEFAULT '',
  `title` varchar(255) NOT NULL DEFAULT '',
   `album` varchar(255) NOT NULL DEFAULT '',
  `label` varchar(255) NOT NULL DEFAULT '',
 `pline` varchar(50) NOT NULL DEFAULT '',
  `trackno` int(11) NOT NULL DEFAULT '0',
  `composer` varchar(100) NOT NULL DEFAULT '',
  `ISRC` varchar(50) NOT NULL DEFAULT '',
  `catalog` varchar(50) NOT NULL DEFAULT '',
   `UPC` varchar(50) NOT NULL DEFAULT '',
  `feeagency` varchar(20) NOT NULL DEFAULT '',
  `albumyear` varchar(4) NOT NULL DEFAULT '0',
  `genre` varchar(20) NOT NULL DEFAULT '',
  `website` varchar(255) NOT NULL DEFAULT '',
  `buycd` varchar(255) NOT NULL DEFAULT '',
   `info` text,
   `lyrics` text,
   `picture` varchar(255) NOT NULL DEFAULT '',
   `count_played` mediumint(9) NOT NULL DEFAULT '0',
   `count_requested` mediumint(9) NOT NULL DEFAULT '0',

    PRIMARY KEY (`ID`),
     UNIQUE KEY `filename` (`filename`),
      KEY `date_played` (`date_played`),
     KEY `date_artist_played` (`date_artist_played`),
    KEY `date_album_played` (`date_album_played`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=347 ;

Now basically on every song i can show the data making the call to history list. but i need to use the historylist id its pulling. to then connect to the artistlist id so it pulls the lyrics field

hope that helps more

Seems like your join condition is incorrect.

ON historylist.ID = artistlist.ID

If this to be work both table have to have 1-1 relationship. Looking at your table structure i think correct condition would be

ON historylist.artist = artistlist.artist

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