Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [mysql] łączenie tabel
peter13135
post
Post #1





Grupa: Zarejestrowani
Postów: 1 447
Pomógł: 191
Dołączył: 26.03.2008

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


struktura tabel:

portal forums
id | cat | name | access

portal topics
id | forum | name | views | autor | last_post | date | closed | sticky

chciałbym aby w wyniku zapytania były takie kolumny:

id | cat | name | access | num_topics* | lt_name** | lt_id***

* liczba rekordow w tabeli portal_topics where topics.forum=forums.id
** pole topics.name w rekordzie gdzie data ma najwiekszą wartość
*** to samo co wyżej z tym że topics.id

lt to tak jakby skrót od last_topic, chciałbym pobrać id oraz name ostatniego tematu

jak narazie wykminiłem coś takiego

  1. SELECT f.* , count( t.id ) AS num_topics, lt.name AS lt_name, lt.id AS lt_id
  2. FROM `portal_forums` f, `portal_topics` t, `portal_topics` lt
  3. WHERE f.id = t.forum AND lt.forum=f.id AND (...)
  4. GROUP BY f.id ASC


i niewiem co dalej.. może mi ktoś to napisać poprawnie ?

Ten post edytował peter13135 26.07.2009, 18:37:18
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
peter13135
post
Post #2





Grupa: Zarejestrowani
Postów: 1 447
Pomógł: 191
Dołączył: 26.03.2008

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


  1. CREATE TABLE IF NOT EXISTS `portal_forums` (
  2. `id` int(11) NOT NULL,
  3. `cat` int(11) NOT NULL,
  4. `name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  5. `access` int(11) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  8.  
  9. --
  10. -- Zrzut danych tabeli `portal_forums`
  11. --
  12.  
  13. INSERT INTO `portal_forums` (`id`, `cat`, `name`, `access`) VALUES
  14. (1, 1, 'forum nr 1 ', 0),
  15. (2, 1, 'forum nr 2', 1),
  16. (3, 2, 'forum 3', 2),
  17. (4, 2, 'forum 4', 8),
  18. (5, 1, 'forumik', 9);
  19.  
  20.  
  21. CREATE TABLE IF NOT EXISTS `portal_topics` (
  22. `id` int(11) NOT NULL AUTO_INCREMENT,
  23. `forum` int(11) NOT NULL,
  24. `name` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
  25. `views` int(11) NOT NULL,
  26. `autor` int(11) NOT NULL,
  27. `last_post` int(11) NOT NULL,
  28. `date` datetime NOT NULL,
  29. `closed` tinyint(1) NOT NULL DEFAULT '0',
  30. `sticky` tinyint(1) NOT NULL DEFAULT '0',
  31. PRIMARY KEY (`id`)
  32. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5061 ;
  33.  
  34. --
  35. -- Zrzut danych tabeli `portal_topics`
  36. --
  37.  
  38. INSERT INTO `portal_topics` (`id`, `forum`, `name`, `views`, `autor`, `last_post`, `date`, `closed`, `sticky`) VALUES
  39. (5018, 1, 'pierwszy temat :)', 12, 8, 8, '2009-07-17 18:09:59', 1, 1),
  40. (5019, 1, 'drugi temat', 19, 8, 12, '2009-07-16 10:33:06', 1, 1),
  41. (5020, 1, 'trzeci temat', 218, 8, 8, '2009-07-25 19:24:49', 0, 1),
  42. (5021, 2, 'z', 9, 8, 8, '2009-07-20 11:45:49', 0, 0),
  43. (5022, 1, 'asd', 2, 8, 8, '2009-07-17 16:43:39', 0, 0),
  44. (5023, 1, 'zz', 1, 8, 8, '2009-07-17 18:24:12', 0, 0),
  45. (5024, 1, 'aaa', 1, 8, 8, '2009-07-17 18:24:16', 0, 0),
  46. (5025, 1, '12', 1, 8, 8, '2009-07-17 18:24:20', 0, 0),
  47. (5026, 1, '123', 1, 8, 8, '2009-07-17 18:24:24', 0, 0),
  48. (5027, 1, '123', 2, 8, 8, '2009-07-17 18:25:01', 0, 0),
  49. (5028, 1, 'addas', 1, 8, 8, '2009-07-17 18:24:30', 0, 0),
  50. (5029, 1, 'sad', 1, 8, 8, '2009-07-17 18:24:57', 0, 0),
  51. (5030, 1, 'asd', 1, 8, 8, '2009-07-17 18:25:08', 0, 0),
  52. (5031, 1, 'asd', 1, 8, 8, '2009-07-17 18:25:12', 0, 0),
  53. (5032, 1, 'asd', 1, 8, 8, '2009-07-17 18:25:15', 0, 0),
  54. (5033, 1, 'ds', 1, 8, 8, '2009-07-17 18:25:19', 0, 0),
  55. (5034, 1, 'asd', 2, 8, 8, '2009-07-17 18:25:28', 0, 0),
  56. (5035, 1, '17', 1, 8, 8, '2009-07-17 18:25:32', 0, 0),
  57. (5036, 1, '18', 1, 8, 8, '2009-07-17 18:25:36', 0, 0),
  58. (5037, 1, '19', 1, 8, 8, '2009-07-17 18:25:40', 0, 0),
  59. (5038, 1, '20', 1, 8, 8, '2009-07-17 18:25:44', 0, 0),
  60. (5039, 1, '21', 1, 8, 8, '2009-07-17 18:26:03', 0, 0),
  61. (5040, 1, '22', 4, 8, 11, '2009-07-18 12:58:21', 0, 0),
  62. (5041, 1, 'za', 1, 11, 11, '2009-07-18 14:22:03', 0, 0),
  63. (5042, 1, 'zaaa', 1, 11, 11, '2009-07-18 14:23:11', 0, 0),
  64. (5043, 1, 'asdasd', 1, 11, 11, '2009-07-18 14:23:41', 0, 0),
  65. (5044, 1, 'asd', 1, 11, 11, '2009-07-18 14:32:47', 0, 0),
  66. (5045, 1, 'd', 5, 11, 11, '2009-07-18 17:20:26', 1, 0),
  67. (5046, 1, 'rt', 1, 11, 11, '2009-07-18 17:20:38', 0, 0),
  68. (5047, 1, 'rt', 2, 11, 11, '2009-07-18 17:20:39', 0, 0),
  69. (5048, 1, 'dfgg', 1, 11, 11, '2009-07-18 17:20:45', 0, 0),
  70. (5049, 1, 'dfgg', 6, 11, 8, '2009-07-20 11:48:26', 1, 0),
  71. (5050, 1, 'df', 2, 11, 11, '2009-07-18 17:21:08', 0, 0),
  72. (5051, 1, 'asd', 6, 11, 11, '2009-07-18 17:21:38', 0, 0),
  73. (5054, 2, 'dsfdsf', 14, 8, 11, '2009-07-26 19:44:22', 0, 0),
  74. (5055, 1, 'closed', 16, 8, 8, '2009-07-20 11:51:36', 0, 1),
  75. (5056, 1, 'zas', 11, 8, 11, '2009-07-24 13:57:33', 1, 0),
  76. (5057, 5, 'zz', 3, 9, 9, '2009-07-23 11:57:20', 0, 0),
  77. (5058, 3, 'zzzz', 4, 8, 8, '2009-07-24 10:05:22', 0, 0),
  78. (5059, 3, 'aa', 4, 8, 8, '2009-07-24 10:06:59', 0, 0),
  79. (5060, 4, 'zzz', 2, 8, 8, '2009-07-24 15:39:30', 0, 0);





edit://
kod który działa, i pobiera mi część pól które chce wygląda tak

  1. SELECT f . * , count( t.id ) AS num_topics
  2. FROM `portal_forums` f, `portal_topics` t
  3. WHERE f.id = t.forum
  4. GROUP BY f.id ASC


Ten post edytował peter13135 26.07.2009, 18:51:43
Go to the top of the page
+Quote Post

Posty w temacie


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: 27.12.2025 - 05:57