views:

92

answers:

1

I am having so much domain name in my DB, for example

www.yahoo.com/duniya.html
www.yahoo.com/hero.html
www.123musiq.com/
www.123musiq.com/home.html
www.123musiq.com/horo.html
www.123musiq.com/yuva.html
www.sensongs.com/
www.sensongs.com/hindi.html
www.sensongs.com/yuva.html
www.sensongs.com/duniya.html
www.sensongs.pk/duniya1.html

i need to sort them like,first 2 from yahoo,then next 2 from 123musiq then next 2 from sensongs. how can i do that? in mysql or PHP? i know to get the domain name,i need to know how to sort 2 from each domain?
i am using match against method

Expecting output

 
    www.yahoo.com/duniya.html
    www.yahoo.com/hero.html
    www.123musiq.com/
    www.123musiq.com/home.html
    www.sensongs.com/
    www.sensongs.com/hindi.html
  

I Use following Code
SELECT x.url
  FROM (SELECT t.url,
               CASE 
                 WHEN @domain = SUBSTRING_INDEX(t.url, '/', 1) THEN @rownum := @rownum + 1
                 ELSE @rownum := 1
               END AS rank,
               @domain := SUBSTRING_INDEX(t.url, '/', 1)
          FROM URL2 t
          JOIN (SELECT @rownum := 0, @domain := '') r
      ORDER BY SUBSTRING_INDEX(t.url, '/', 1)) X
 WHERE x.rank <= 2 AND MATCH(teet,url,html) AGAINST ('account');

It Showing Error
Query : SELECT x.url   FROM (SELECT t.url,                CASE                   WHEN @domain = SUBSTRING_INDEX(t.url, '/', 1) THEN @row...
Error Code : 1054
Unknown column 'teet' in 'where clause'

But Teet is a Column in my URL2 Table

+2  A: 

MySQL doesn't have analytic functions, which is what you'd need to get the two entries per grouping. The other pain is the "www." complicates locating the 2nd period...

Use:

SELECT x.url
  FROM (SELECT t.url,
               t.teet,
               t.html,
               CASE 
                 WHEN @domain = SUBSTRING_INDEX(t.url, '/', 1) THEN @rownum := @rownum + 1
                 ELSE @rownum := 1
               END AS rank,
               @domain := SUBSTRING_INDEX(t.url, '/', 1)
          FROM URL2 t
          JOIN (SELECT @rownum := 0, @domain := '') r
      ORDER BY SUBSTRING_INDEX(t.url, '/', 1)) x
 WHERE x.rank <= 2
OMG Ponies
@OMG: we can find the domain by http://stackoverflow.com/questions/3521094/how-we-can-find-domain-name-using-mysql-and-regular-expression ,wht is x.domainname,x.rank,t.domainname
Alex Mathew
@Alex Mathew: Thx, updated. `x` is the table alias for the derived table/inline view. It's used the same way as the `t` alias is in that derived table/inline view...
OMG Ponies
@OMG: My table name is url2 and coloum name for saving url is url,can u rewrite the SQL?
Alex Mathew
@Alex Mathew: Updated answer
OMG Ponies
@OMG: when i add a MATCH AGAINST IT is Showing error
Alex Mathew
@Alex Mathew: I don't understand what you mean by "MATCH AGAINST IT is Showing error" - what error #? And can you update the question to show how you are attempting to use the query regarding the "MATCH AGAINST"?
OMG Ponies
@OMG: Question Updated
Alex Mathew
@Alex Mathew: The derived table only contains two columns--`url` and `rank`. Assuming they exist in the `URL2` table, you need to expose them in the derived table for them to be visible to the FTS MATCH/AGAINST clause you listed - see update.
OMG Ponies
@OMG:I am Not a DB DEVELOPER, i am just a PHP Programmer, can you please update the query with MATCH/AGAINST,I tried but it is showing there no column name t.url,but it is in the INDEX
Alex Mathew
@Alex Mathew: I'm trying to help as best I can, but you have to provide details about what you want and what you are trying to do--I'm not psychic. If you want to access the `url` column outside of the `FROM` clause, you need to use the "x" alias...
OMG Ponies
@OMG: i check that with x alias it showing error, Can't find FULLTEXT index matching the column list
Alex Mathew
@Alex Mathew: Have you created a Full Text Index for each of the columns in that list? IE: `CREATE FULLTEXT INDEX ft_url_idx ON URL2(url)`
OMG Ponies
@OMG: i create FULLTEXT for 3 columns (url,teet,html) in the name "SEARCH"
Alex Mathew
@Alex Mathew: Do you still get the missing full text index error? Where did this full text search requirement come from? It wasn't originally in this question, nor the previous one.
OMG Ponies
@OMG: Ya,i am still getting the missing full text index, actually i forget to add this INDEX need first,sorry
Alex Mathew
@Alex Mathew: I'm grasping at straws, but you could move the Full Text search into the inner query... but that would affect the rank values, because the rank would be only those that match based on the full text - currently it's ranking based on the domain name.
OMG Ponies
@OMG:I Add that in the Inner query that also not working :(
Alex Mathew
OMG Ponies