Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SQL]Zapytanie SQL JOIN
Forum PHP.pl > Forum > Przedszkole
sannin
Witam,

chcę wybrać z bazy użytkowników którzy przez ostatnie 30 dni nie dodali żadnego zdjęcia. Zbudowałem takie zapytanie

Kod
SELECT user_name FROM cpg14x_users JOIN cpg14x_pictures ON (ctime < (NOW() - 2592000))


Ale zwraca mi loginy wszystkich userów czy coś takiego
Cytat
(NOW() - 2592000))
jest błędem?
nieraczek
JOIN questionmark.gif

Może to i zadziała, ale wydaje mi się, że stosujemy to tylko do łączenia tabel, ja bym uzył WHERE, poza tym data w jakim formacie jest w kolumnie 'ctime' - wykonaj w bazie danych zapytanie:
  1. SELECT NOW( ) -2592000


zwróci np. 20090503628148.000000 - twoja kolumna 'ctime' zawiera date i czas na pewno w takim formacie ? tongue.gif
sannin
a jak inaczej wybrać dane z dwóch tabel? W kolumnie ctime jest przechowywany czas dodania zdjęcia wywołany za pomocą time()
kefirek
Złączasz tabele za pomocą LEFT JOIN najlepiej pokaż strukture tabeli
sannin
Zamiast
Kod
NOW()
powinno być
Kod
SELECT unix_timestamp(now())


Cytat(kefirek @ 6.05.2009, 22:14:14 ) *
Złączasz tabele za pomocą LEFT JOIN najlepiej pokaż strukture tabeli


Tak właśnie robię, ale wywala mi wszystkich użytkowników

Kod
SELECT cpg14x_users.user_name FROM cpg14x_users LEFT JOIN (cpg14x_pictures) ON (cpg14x_users.user_id = cpg14x_pictures.owner_id AND cpg14x_pictures.ctime < 1239046922) GROUP by cpg14x_users.user_name


Wygląda na to, że ignoruje to
Kod
cpg14x_users.user_id = cpg14x_pictures.owner_id AND cpg14x_pictures.ctime < 1239046922
bo nawet jak wpiszę tam sprzeczne dane to wyświetla wszystkich. Struktura tabeli
Kod
--
-- Struktura tabeli dla  `cpg14x_users`
--

CREATE TABLE `cpg14x_users` (
   `user_id` int(11) NOT NULL auto_increment,
   `user_group` int(11) NOT NULL default '2',
   `user_active` enum('YES','NO') collate utf8_unicode_ci NOT NULL default 'NO',
   `user_name` varchar(25) collate utf8_unicode_ci NOT NULL,
   `user_password` varchar(40) collate utf8_unicode_ci NOT NULL,
   `user_lastvisit` datetime NOT NULL default '0000-00-00 00:00:00',
   `user_regdate` datetime NOT NULL default '0000-00-00 00:00:00',
   `user_group_list` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_email` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_profile1` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_profile2` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_profile3` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_profile4` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_profile5` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_profile6` text collate utf8_unicode_ci NOT NULL,
   `user_profile7` text collate utf8_unicode_ci NOT NULL,
   `user_actkey` varchar(32) collate utf8_unicode_ci NOT NULL,
   `auto_subscribe_post` tinyint(4) NOT NULL default '1',
   `auto_subscribe_comment` tinyint(4) NOT NULL default '1',
   `avatar_url` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_pmsmail` tinyint(4) NOT NULL default '1',
   `enable_admin_email` tinyint(4) NOT NULL default '1',
   `picid_url` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user_sub` varchar(25) collate utf8_unicode_ci NOT NULL,
   PRIMARY KEY  (`user_id`),
   UNIQUE KEY `user_name` (`user_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Used to store users, not used when bridged' AUTO_INCREMENT=929;


Kod
CREATE TABLE `cpg14x_pictures` (
   `pid` int(11) NOT NULL auto_increment,
   `aid` int(11) NOT NULL default '0',
   `filepath` varchar(255) collate utf8_unicode_ci NOT NULL,
   `filename` varchar(255) collate utf8_unicode_ci NOT NULL,
   `filesize` int(11) NOT NULL default '0',
   `total_filesize` int(11) NOT NULL default '0',
   `pwidth` smallint(6) NOT NULL default '0',
   `pheight` smallint(6) NOT NULL default '0',
   `hits` int(10) NOT NULL default '0',
   `mtime` datetime NOT NULL default '0000-00-00 00:00:00',
   `ctime` int(11) NOT NULL default '0',
   `owner_id` int(11) NOT NULL default '0',
   `owner_name` varchar(40) collate utf8_unicode_ci NOT NULL,
   `pic_rating` int(11) NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',
   `title` varchar(255) collate utf8_unicode_ci NOT NULL,
   `caption` text collate utf8_unicode_ci NOT NULL,
   `keywords` varchar(255) collate utf8_unicode_ci NOT NULL,
   `approved` enum('YES','NO') collate utf8_unicode_ci NOT NULL default 'NO',
   `galleryicon` int(11) NOT NULL default '0',
   `user1` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user2` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user3` varchar(255) collate utf8_unicode_ci NOT NULL,
   `user4` varchar(255) collate utf8_unicode_ci NOT NULL,
   `url_prefix` tinyint(4) NOT NULL default '0',
   `pic_raw_ip` tinytext collate utf8_unicode_ci,
   `pic_hdr_ip` tinytext collate utf8_unicode_ci,
   `lasthit_ip` tinytext collate utf8_unicode_ci,
   `position` int(11) NOT NULL default '0',
   PRIMARY KEY  (`pid`),
   KEY `pic_hits` (`hits`),
   KEY `pic_rate` (`pic_rating`),
   KEY `aid_approved` (`aid`,`approved`),
   KEY `pic_aid` (`aid`),
   KEY `owner_id` (`owner_id`),
   FULLTEXT KEY `search` (`title`,`caption`,`keywords`,`filename`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Used to store data about individual pics' AUTO_INCREMENT=2079;
nieraczek
A WHERE questionmark.gif

  1. SELECT u.user_name FROM cpg14x_users u
  2. LEFT JOIN cpg14x_pictures p ON u.user_id = p.owner_id
  3. WHERE p.ctime < 1239046922
  4. GROUP BY u.user_name
sannin
Dzięki wiedziałem, że coś zrobiłem źle smile.gif Tylko nadal jest problem bo wyświetla mi około 120 userów, ale tam też Ci którzy dodali pracę w przeciągu ostatnich 30 dni. Musi być gdzieś błąd w zapytaniu.

Kod
SELECT u.user_name
FROM cpg14x_users u
LEFT JOIN cpg14x_pictures p ON u.user_id = p.owner_id
WHERE p.ctime < ( unix_timestamp( now( ) ) -2592000 )
GROUP BY u.user_name
LIMIT 90 , 30


Jedni użytkownicy dodali po kilkaset zdjęć, a inni znów ani jednego. Gdzieś tutaj popełniłem chyba błąd.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.