Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SQL]Zapytanie SQL JOIN
sannin
post
Post #1





Grupa: Zarejestrowani
Postów: 308
Pomógł: 13
Dołączył: 31.10.2008

Ostrzeżenie: (0%)
-----


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?

Ten post edytował sannin 6.05.2009, 20:43:29
Go to the top of the page
+Quote Post
nieraczek
post
Post #2





Grupa: Zarejestrowani
Postów: 405
Pomógł: 6
Dołączył: 12.01.2007

Ostrzeżenie: (0%)
-----


JOIN (IMG:http://forum.php.pl/style_emoticons/default/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 ? (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
sannin
post
Post #3





Grupa: Zarejestrowani
Postów: 308
Pomógł: 13
Dołączył: 31.10.2008

Ostrzeżenie: (0%)
-----


a jak inaczej wybrać dane z dwóch tabel? W kolumnie ctime jest przechowywany czas dodania zdjęcia wywołany za pomocą time()
Go to the top of the page
+Quote Post
kefirek
post
Post #4





Grupa: Zarejestrowani
Postów: 781
Pomógł: 256
Dołączył: 29.06.2008

Ostrzeżenie: (0%)
-----


Złączasz tabele za pomocą LEFT JOIN najlepiej pokaż strukture tabeli
Go to the top of the page
+Quote Post
sannin
post
Post #5





Grupa: Zarejestrowani
Postów: 308
Pomógł: 13
Dołączył: 31.10.2008

Ostrzeżenie: (0%)
-----


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;


Ten post edytował sannin 6.05.2009, 22:00:02
Go to the top of the page
+Quote Post
nieraczek
post
Post #6





Grupa: Zarejestrowani
Postów: 405
Pomógł: 6
Dołączył: 12.01.2007

Ostrzeżenie: (0%)
-----


A WHERE (IMG:http://forum.php.pl/style_emoticons/default/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
Go to the top of the page
+Quote Post
sannin
post
Post #7





Grupa: Zarejestrowani
Postów: 308
Pomógł: 13
Dołączył: 31.10.2008

Ostrzeżenie: (0%)
-----


Dzięki wiedziałem, że coś zrobiłem źle (IMG:http://forum.php.pl/style_emoticons/default/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.

Ten post edytował sannin 7.05.2009, 18:27:54
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 05:15