Difference between revisions of "Talk:SQL"

From Organic Design wiki
(Selecting current articles by category)
(Notes on correct query)
 
(3 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
*hooks used
 
*hooks used
 
*hooks defined
 
*hooks defined
:Absolutely --[[User:Sven|Sven]] 15:55, 11 February 2008 (NZDT)
 
  
==Selecting current articles by category==
+
==Building up the query==
See [http://upload.wikimedia.org/wikipedia/commons/4/41/Mediawiki-database-schema.png MediaWiki schema] for a description of the tables. Essentially '''categorylinks''' store the category member relationships , '''page''' identifies the title and metadata information, and '''text''' contains the actual wikitext of articles.
+
: SELECT page_latest FROM page, categorylinks
 +
: WHERE cl_to = "Portal" AND cl_from = page_id;
  
The MediaWiki allows variable article content to be stored as key => value pairs where the atomic unit for the content varies depending on the content of the article. A way around this is to use categorization to group common atomic unit structure together. Basically this is a filtering problem, however queried atomic unit structure needs to be checked downstream,
+
:SELECT rev_text_id FROM revision
 +
: WHERE rev_id IN (SELECT page_latest FROM page, categorylinks WHERE cl_to = "Portal" AND cl_from = page_id);
  
 +
;This is slow dues to a lack of indexing
 +
:SELECT * FROM text WHERE old_id IN (SELECT rev_text_id FROM revision
 +
:WHERE rev_id IN (SELECT page_latest FROM page, categorylinks WHERE cl_to = "Portal" AND cl_from = page_id));
  
Example;
+
;Two parts step 1;
* Category: [[:Category:Rosaceae]]
+
:SELECT page_latest FROM page, categorylinks WHERE cl_to = "Portal" AND cl_from = page_id)
* Article: [[Rosaceae]] (Latest revision number is 1352)
+
;step 2
 +
:SELECT * FROM text WHERE old_id IN (SELECT rev_text_id FROM revision
 +
:WHERE rev_id IN (3031, 3033)
  
<pre>
+
;Select statement that works
SELECT  * from categorylinks WHERE cl_to like 'Rosaceae';
+
:SELECT  old_text FROM {{SQLprefix}}text, {{SQLprefix}}revision
</pre>
+
:WHERE old_id = rev_text_id and rev_id IN (
 
+
SELECT page_latest FROM {{SQLprefix}}page, {{SQLprefix}}categorylinks
;This selects all pages (article names) from category ''Rosaceae''.
+
WHERE cl_to = "Portal" and cl_from = page_id );
<pre>
 
SELECT * from page
 
WHERE page_id IN (SELECT  cl_from from categorylinks WHERE cl_to like 'Rosaceae');
 
</pre>
 
 
 
;This selects all revisions of articles from category ''Rosaceae''.
 
<pre>
 
SELECT * FROM revision  
 
WHERE rev_page IN (SELECT  cl_from from categorylinks WHERE cl_to like 'Rosaceae');
 
</pre>
 
From the output of this information it can be seen that what we want is the rev_text_id that is largest for any rev_page.
 
 
 
;This selects all text from articles of category ''Rosaceae'';
 
<pre>
 
Select * from text
 
WHERE old_id IN (SELECT page_latest from page WHERE page_id IN (SELECT  cl_from from categorylinks WHERE cl_to like 'Rosaceae'));
 
</pre>
 
 
 
;Nads approach for quicker query
 
<pre>
 
select text from catlinks,text where catlinks.cl_to = "cat" and text.id = cat.id
 
</pre>
 

Latest revision as of 03:53, 27 May 2008

I need to add links to an article I have on SQL, I also propose a section listing every mediawiki extension that contains SQL queries --Sven 14:09, 11 February 2008 (NZDT)

Sounds like a job for SMW - adding script related properties to them all. Or maybe to their talk pages since scripts aren't wikitext articles - or could add a namespace for that - like properties:? --Nad 14:26, 11 February 2008 (NZDT)

Ideas for script-related properties

  • has db queries
  • classes/methods used
  • classes defined
  • hooks used
  • hooks defined

Building up the query

SELECT page_latest FROM page, categorylinks
WHERE cl_to = "Portal" AND cl_from = page_id;
SELECT rev_text_id FROM revision
WHERE rev_id IN (SELECT page_latest FROM page, categorylinks WHERE cl_to = "Portal" AND cl_from = page_id);
This is slow dues to a lack of indexing
SELECT * FROM text WHERE old_id IN (SELECT rev_text_id FROM revision
WHERE rev_id IN (SELECT page_latest FROM page, categorylinks WHERE cl_to = "Portal" AND cl_from = page_id));
Two parts step 1;
SELECT page_latest FROM page, categorylinks WHERE cl_to = "Portal" AND cl_from = page_id)
step 2
SELECT * FROM text WHERE old_id IN (SELECT rev_text_id FROM revision
WHERE rev_id IN (3031, 3033)
Select statement that works
SELECT old_text FROM 1120_text, 1120_revision
WHERE old_id = rev_text_id and rev_id IN (
SELECT page_latest FROM 1120_page, 1120_categorylinks
WHERE cl_to = "Portal" and cl_from = page_id );