selectare din baza de date
Last Updated: Apr 10 2020 11:34, Started by
bunicu9
, Apr 09 2020 18:13
·
0
#1
Posted 09 April 2020 - 18:13
Vreau sa implementez pe site-ul meu un sistem de tag-uri.
CREATE TABLE `post` ( `id` smallint(6) NOT NULL, `name` varchar(100) NOT NULL, `body` text NOT NULL, `tags` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Fiecare post are mai multe taguri: text1, text2, text3 Cum pot selecta postul care contine un anumit text in tags chiar daca sunt mai multe Eu am incercat asa: $Statement = $Database->prepare(sprintf("SELECT id FROM %s WHERE tags = '%s' ", Attribute::TABLE, $tags)); Eu ii dau o valoare lui $tags = text1 dar imi gaseste doar postul care are un singur tags cu exact text1. Cum le pot gasi si pe alea care au text1, text2 ? |
#2
Posted 09 April 2020 - 18:19
tags LIKE '%test1%'
Nu-i insa cea mai buna abordare Apoi faci prepare la statement sa poti oferi cum trebuie parametri, sa scapi de sprintf, nu sa le amesteci. |
#3
Posted 09 April 2020 - 20:25
ce ai tu nevoie se numeste full text search. cauta cum se implementeaza in tipul de sql pe care il folosesti.
|
#4
Posted 09 April 2020 - 21:01
ciuschi, on 09 aprilie 2020 - 20:25, said:
ce ai tu nevoie se numeste full text search. cauta cum se implementeaza in tipul de sql pe care il folosesti. ALTER TABLE `post` ADD FULLTEXT KEY `search` (`name`, `body`, `tags`); Dar vreau sa fac ceva separat sa caute doar in tags nu si in name si in body $Statement = $Database->prepare(sprintf("SELECT id FROM %s WHERE MATCH(tags) AGAINST(? IN BOOLEAN MODE) LIMIT 20", Attribute::TABLE));General error: 1191 Can't find FULLTEXT index matching the column list |
#5
Posted 10 April 2020 - 11:34
Nu e bine să faci fulltext search.
Cel mai bine e sa faci un tabel cu taguri (nume și id unice) Si încă un tabel de asociere în care folosești doar id-uri: de postari și de taguri. Structura acestui tabel e (int, int). Baza de date optimizează foarte bine numerele, mai ales dacă indexezi coloanele respective. În rest, folosești JOIN. |
Anunturi
Bun venit pe Forumul Softpedia!
▶ 0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users