Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> wyszukac '1' w catX i podac ile razy
POLVIP
post 29.12.2003, 22:59:08
Post #1





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 1.11.2003
Skąd: Long Island, NY USA

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


kolejny orzech do zgryzienia
nie jestem pewny czy to można to rozwiązać tylko przy mySQL

mam tabelę z id, cat1,cat2,cat3,cat4...cat76
id to INT(4)
cat1-75 to tinyint(1)
w zależności czy jest lub nie ma jest 0 i 1 np:

id=12, cat1=0, cat2=1, cat3=1, cat4=0....
id=13, cat1=1, cat2=1, cat3=0, cat4=1....

Jak teraz uzyskać wynik na ile 1 jest w kazdym z cat np:

cat1 = 34
cat2 = 44
cat75 = 8

Czy uda sie to zrobic tylko w SELECT?
Jesli nie, jaki bybly najlatwiejszy sposob na połączenie z php?


--------------------
Mariuz @ POLVIP ;)
Znajdziesz mnie tutaj...
Zobacz: http://www.polvip.net
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
AcidBurnt
post 29.12.2003, 23:06:33
Post #2





Grupa: Zarejestrowani
Postów: 215
Pomógł: 1
Dołączył: 13.04.2003
Skąd: z ławki przed blokiem

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


wrzuc sql z dumpa bazy bo zabardzo nie widze tej stroktury
Go to the top of the page
+Quote Post
POLVIP
post 29.12.2003, 23:12:16
Post #3





Grupa: Zarejestrowani
Postów: 80
Pomógł: 0
Dołączył: 1.11.2003
Skąd: Long Island, NY USA

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


[sql:1:8de9302965]
CREATE TABLE `categories` (
`id` int(6) NOT NULL default '0',
`reference_id` int(4) NOT NULL default '0',
`reference_type` varchar(10) NOT NULL default '',
`cat1` tinyint(1) default '0',
`cat2` tinyint(1) default '0',
`cat3` tinyint(1) default '0',
`cat4` tinyint(1) default '0',
`cat5` tinyint(1) default '0',
`cat6` tinyint(1) default '0',
`cat7` tinyint(1) default '0',
`cat8` tinyint(1) default '0',
`cat9` tinyint(1) default '0',
`cat10` tinyint(1) default '0',
`cat11` tinyint(1) default '0',
`cat12` tinyint(1) default '0',
`cat13` tinyint(1) default '0',
`cat14` tinyint(1) default '0',
`cat15` tinyint(1) default '0',
`cat16` tinyint(1) default '0',
`cat17` tinyint(1) default '0',
`cat18` tinyint(1) default '0',
`cat19` tinyint(1) default '0',
`cat20` tinyint(1) default '0',
`cat21` tinyint(1) default '0',
`cat22` tinyint(1) default '0',
`cat23` tinyint(1) default '0',
`cat24` tinyint(1) default '0',
`cat25` tinyint(1) default '0',
`cat26` tinyint(1) default '0',
`cat27` tinyint(1) default '0',
`cat28` tinyint(1) default '0',
`cat29` tinyint(1) default '0',
`cat30` tinyint(1) default '0',
`cat31` tinyint(1) default '0',
`cat32` tinyint(1) default '0',
`cat33` tinyint(1) default '0',
`cat34` tinyint(1) default '0',
`cat35` tinyint(1) default '0',
`cat36` tinyint(1) default '0',
`cat37` tinyint(1) default '0',
`cat38` tinyint(1) default '0',
`cat39` tinyint(1) default '0',
`cat40` tinyint(1) default '0',
`cat41` tinyint(1) default '0',
`cat42` tinyint(1) default '0',
`cat43` tinyint(1) default '0',
`cat44` tinyint(1) default '0',
`cat45` tinyint(1) default '0',
`cat46` tinyint(1) default '0',
`cat47` tinyint(1) default '0',
`cat48` tinyint(1) default '0',
`cat49` tinyint(1) default '0',
`cat50` tinyint(1) default '0',
`cat51` tinyint(1) default '0',
`cat52` tinyint(1) default '0',
`cat53` tinyint(1) default '0',
`cat54` tinyint(1) default '0',
`cat55` tinyint(1) default '0',
`cat56` tinyint(1) default '0',
`cat57` tinyint(1) default '0',
`cat58` tinyint(1) default '0',
`cat59` tinyint(1) default '0',
`cat60` tinyint(1) default '0',
`cat61` tinyint(1) default '0',
`cat62` tinyint(1) default '0',
`cat63` tinyint(1) default '0',
`cat64` tinyint(1) default '0',
`cat65` tinyint(1) default '0',
`cat66` tinyint(1) default '0',
`cat67` tinyint(1) default '0',
`cat68` tinyint(1) default '0',
`cat69` tinyint(1) default '0',
`cat70` tinyint(1) default '0',
`cat71` tinyint(1) default '0',
`cat72` tinyint(1) default '0',
`cat73` tinyint(1) default '0',
`cat74` tinyint(1) default '0',
`cat75` tinyint(1) default '0',
PRIMARY KEY (`reference_id`),
KEY `id` (`id`)
) TYPE=MyISAM;[/sql:1:8de9302965]

tutaj


--------------------
Mariuz @ POLVIP ;)
Znajdziesz mnie tutaj...
Zobacz: http://www.polvip.net
Go to the top of the page
+Quote Post
AcidBurnt
post 29.12.2003, 23:28:55
Post #4





Grupa: Zarejestrowani
Postów: 215
Pomógł: 1
Dołączył: 13.04.2003
Skąd: z ławki przed blokiem

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


[php:1:41b8352ed9]<?php
for($i=1;$i<=76;$i++)
{
$sql = "SELECT * FROM categories WHERE cat".$i." =1";
$res = mysql_query($sql);
$ile = mysql_numrows($res)
$tab[$i] = $ile;
}
?>[/php:1:41b8352ed9]


moze siakos tak?
Go to the top of the page
+Quote Post
Seth
post 30.12.2003, 00:19:41
Post #5





Grupa: Przyjaciele php.pl
Postów: 2 335
Pomógł: 6
Dołączył: 7.03.2002

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


@POLVIP: lolaboga smile.gif... a nie lepije bylo to podzielic na dwie tabele ?
Jedna z:
[sql:1:dc5f100cb2]`id` INT(6) NOT NULL DEFAULT '0',
`reference_id` INT(4) NOT NULL DEFAULT '0',
`reference_type` VARCHAR(10)[/sql:1:dc5f100cb2]

a druga z:
[sql:1:dc5f100cb2]`reference_id` INT(4) NOT NULL DEFAULT '0',
`cat_name` VARCHAR(10) NOT NULL,
`cat_value` TINYINT(1) DEFAULT '0'[/sql:1:dc5f100cb2]

i do tej drugiej wrzucac nazwy kategorii i ich wartosci ?
Go to the top of the page
+Quote Post
spenalzo
post 30.12.2003, 01:45:47
Post #6





Grupa: Zarejestrowani
Postów: 2 064
Pomógł: 1
Dołączył: 22.01.2003
Skąd: Poznań

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


Więc tak - jeżeli to w ogole zadziała to w MySQL 4.1.1
[php:1:29ef791a03]<?php
for($i=1; $i<=75; $i++)
{
$sql[]="( SELECT COUNT(cat".$i.") AS ilosc, 'cat".$i."' AS kat FROM tabela WHERE cat".$i." = 1 GROUP BY cat".$i." )";
}
$sql=implode(" UNION ", $sql);
$sql.=" ORDER BY ilosc DESC";
$res=mysql_query($sql) or die(mysql_error());
while($t=mysql_fetch_array($res))
{
echo $t["kat"]." ".$t["ilosc"]."<br>";
}
?>[/php:1:29ef791a03]

To jest czysta teoria, wiec nie mam pojęcia czy to będzie działać - ale naprawde radze przeprojektować strukture tabeli...

Update: poprawka w SQLu i php (dodane wyswietlanie).


--------------------

Go to the top of the page
+Quote Post
AcidBurnt
post 30.12.2003, 02:11:45
Post #7





Grupa: Zarejestrowani
Postów: 215
Pomógł: 1
Dołączył: 13.04.2003
Skąd: z ławki przed blokiem

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


spenalzo co to " UNION " przy zapytaniu??
Go to the top of the page
+Quote Post
spenalzo
post 30.12.2003, 02:40:08
Post #8





Grupa: Zarejestrowani
Postów: 2 064
Pomógł: 1
Dołączył: 22.01.2003
Skąd: Poznań

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


Sprawdź sam: http://www.mysql.com/doc/en/


--------------------

Go to the top of the page
+Quote Post
spenalzo
post 30.12.2003, 03:18:47
Post #9





Grupa: Zarejestrowani
Postów: 2 064
Pomógł: 1
Dołączył: 22.01.2003
Skąd: Poznań

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


Hmm. Więc tak - zapytanie działa, ale nie wiem czy poprawnie.
POLVIP: sprawdź, czy zapytanie zwraca odpowiednie wartości - ja to zrobiłem dla losowych danych i tak do końca nie wiem czy jest OK.


--------------------

Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 20:34