WN

WN (https://www.wn.se/forum/index.php)
-   Serversidans teknologier (https://www.wn.se/forum/forumdisplay.php?f=4)
-   -   Någon som är bra på sql (https://www.wn.se/forum/showthread.php?t=10101)

vco-systems 2005-09-29 21:58

Jag har följande kodsnutt i en sökfunktion.
Hur kan jag fixa så att jag varje "page_id" bara finns en gång i resultatet?

Kod:

SELECT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
)

Finns det något enkelt sätt eller måste jag "parsa" resultatet i efterhand?

chrizz 2005-09-29 22:11

Hur du än gör så kommer ju page_id finns i alla rader som queryn ger. Vill du bara få resultat för EN rad eller, även om det finns flera? Tror inte jag förstår frågan helt korrekt annars.

vco-systems 2005-09-29 22:55

Nu kan jag få ett resultat som ser ut så här:

Kod:

page_id  subtitle  etc...
24      x
62      y
62      z
62      a
62      b

Det jag vill ha är:

page_id  subtitle  etc...
24      x
62      y

Dvs. varje värde på page_id ska bara finnas 1 gång.

mrnoname 2005-09-29 23:05

GROUP BY page_id

Kolla på http://www.w3schools.com/sql/sql_groupby.asp

chrizz 2005-09-30 00:16

SELECT DISTINCT nuke_paged_content.page_id AS page_id .... fungerar nog lika bra som GROUP BY, men jag hade kört GROUP BY ändå.

Mao:

Kod:


SELECT DISTINCT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
)

ger samma som:


Kod:


SELECT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
GROUP BY page_id
)

säger jag utan att ha provat =)

vco-systems 2005-09-30 07:40

Citat:

Originally posted by chrizz@Sep 30 2005, 00:16
SELECT DISTINCT nuke_paged_content.page_id AS page_id .... fungerar nog lika bra som GROUP BY, men jag hade kört GROUP BY ändå.

Mao:

Kod:


SELECT DISTINCT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
)

ger samma som:


Kod:


SELECT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
GROUP BY page_id
)

säger jag utan att ha provat =)

Citat:


Kod:


SELECT DISTINCT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
)


Har jag testat tidigare och det fungerar inte.

Citat:


Kod:


SELECT nuke_paged_content.page_id AS page_id, nuke_paged_content.subtitle AS subtitle, nuke_paged_content.text AS text, nuke_paged_titles.title AS title, nuke_paged_titles.ingress AS ingress, nuke_paged_titles.topic_id AS topic_id
FROM nuke_paged_content
LEFT JOIN nuke_paged_titles ON ( nuke_paged_content.page_id = nuke_paged_titles.page_id )
WHERE (
nuke_paged_content.subtitle
LIKE "%sökord%" OR nuke_paged_titles.title
LIKE "%sökord%" OR nuke_paged_titles.ingress
LIKE "%sökord%" OR nuke_paged_content.text
LIKE "%sökord%"
GROUP BY page_id
)


Fungerar utmärkt om man flyttar GROUP BY efter parantesen.

Tack så mycket :D

chrizz 2005-09-30 13:23

Hm, distinct borde ju fungera. Nåja... =)

missade parantesen dock ;)


Alla tider är GMT +2. Klockan är nu 15:10.

Programvara från: vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Svensk översättning av: Anders Pettersson