Subquery with title in Semantic MediaWiki - mediawiki

I have 2 template:
- Relations
- name1
- name2
and
- People
- mainlabel ( the name of the guy as title of my page )
- age
- etc..
Ok, now I would find every people friend of "MrX", and retrieve his personal information ( like age etc.. )
The query doesn't work, but i think it explains the concept:
{{#ask: [[Category:People]] [[**NAME OF PRESENT GUY**::<q>[[name1::{{SUBJECTPAGENAME}}]]</q>]]
|?
|?age
|?eyes
}}
where SUBJECTPAGENAME="MrX"
Is it possible?

Assuming that your property is called Is a friend of:
{{#ask: [[Category:People]] [[Is a friend of::MrX]]
|?age
|?eyes
}}
The syntax is always Property::Value. I don't know what you mean by “SUBJECTPAGENAME”, but you can use the name of the current page like this
Assuming that your property is called Is a friend of:
{{#ask: [[Category:People]] [[Is a friend of::{{PAGENAME}}]]
|?age
|?eyes
}}

Related

How do I get all strings that do not contain another string in MySQL?

I have a table called "Domains" with field "Name" (unique, always lowercase) which contains a list of domains and subdomains on my server like:
blah.example.com
www.example.com
www.blah.example.com
example.com
example.nl
example.org
Looking at this list, names 1, 2 and 3 are subdomains of item 4. And I'm looking to just find all domains in this table without these subdomains. Or, to be more precise, any name that does not have part of it in the name from another record. Thus only item 4, 5 and 6.
If record 4 was missing then this query would also have item 1 and 2 as result, but not item 3. After all, item 3 has item 1 as part of it.
Just trying to find the query that can provide me this result... Something with select d.name from domains where d.name not in... Well, there my mind goes blank.
Why?
This list of domains is generated by my web server which registers every new domain that gets requested on it. I'm working on a reporting page where I would display the top domain names to see if there are any weird domains in it. For some reason, I sometimes see unknown domain names in these requests and this might give some additional insight in it all.
I am going to change my code so it will include references to parent domains in the same table in the future but for now I'll have to deal with this and a simple SQL solution.
Use a self-join that matches on suffixes using LIKE
SELECT d1.name
FROM domains AS d1
LEFT JOIN domains AS d2 ON d1.name LIKE CONCAT('%.', d2.name)
WHERE d2.name IS NULL
DEMO

SQL\ HTML Databases

I was actually doing somthing else and came accross this intresing W3schools tutorial/ section in relation to SQL databases (its a curiousity killed the cat thing more then anything)
The page link where my question comes from is as follows
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where
the example lists all records that include mexico in the countrys catagorie
I can for instance change this to spain and all entrys with spain are filterd through.
The question I have is, it seems very unlikely to me that an end user is going to type out the entire code
SELECT * FROM Customers
WHERE Country='Mexico';
everytime they want too search a city (this example thus being mexico). I'm presuming we can get the city name inserted from a textbox, but ive looked can cant fand any examples on how to do this?
Try this:
SELECT * FROM Customers where <Column> like '%MEXICO%';
'%' is the wildcard. So you will find:
" MEXICO"
"MEXICO City"
"MX MEXICO"
If you want to use the wildcard you must use 'like' instead of '='

SQL To Pull Linked List(ish) Data From Tables

Thank you in advance for your help...
I want to get the parent site list/informatoin for a site based on its ID. There is a site hierarchy described in 2 tables, like this:
table: site
- id (PK)
- name
table: hierarchy
- siteid (PK) (siteid => site.id)
- parented (PK) (parented => site.id)
- topid (topid => site.id)
For example, you may have the following data in the tables:
site:
id,name
0,Earth
1,US
2,NY
3,GA
4,Queens
5,Farmers' Market
6,Buckhead
hierarchy:
siteid,parented,topid
5,4,1
4,2,1
2,1,1
1,0,1
5,6,1
6,3,1
3,1,1
1,0,1
I want to get all the parents of the Farmers's Market sites. There are markets in both Buckhead and Queens.
How do I make SQL walk / recurse up the tree to get something like this if I want the parent list for site id=5?:
site_name,parent_name
Farmers' Market,Queens
Queens,NY
NY,Earth
Farmers' Market,Buckhead
Buckhead,GA
GA,Earth
It occurs to me that there may need to be more than one row in the site table for Farmers' Market, but, I don't think so...
Thanks for your help,
David

Pictures & Tags - Database Design Question

I have 3 tables which allows me to get all tags of some picture, or all pictures with a specific tag.
I would like also to know the number of times each tag exist. How could I get this info using MySQL ?
Is it worth to add a "Count" column to the Tags table to hold this info ?
Files Table
File ID File Name ...
------- ---------
1 a.jpg ...
2 b.png ...
3 c.jpg ...
. .
. .
. .
Tags Table
Tag Name Tag Creator ...
-------- -----------
David david ...
2010 julia ...
. .
. .
. .
FilesTags Table
File ID Tag Name
------- --------
1 2010
1 April
1 David
2 2010
2 Julia
3 Friends
. .
. .
. .
SELECT FilesTags.Tag_name, COUNT(*) as number_of_occurences
FROM FilesTags
GROUP BY FilesTags.Tag_name;
Zaki's response answers one of your questions but not the other one.
"Is it worth to add a "Count" column
to the Tags table to hold this info ?"
It really depends on what you are going to do with the counts. Which in turn depends on the nature of your web site.
SO makes displays the counts alongside the tags; that's partly because its target audience is geeks and geeks like numbers. But also the count is useful information when it comes to tagging a question. Plus there's the Taxonomist's badge to consider, and the display of new tags on the Moderators' page. In other words, there's a lot of calls on the count, so it makes sense to hold and maintain the count against each tag.
Other sites don't extract nearly as much juice out of their tags. But, as an example, say you want to display a tag cloud; you might want to be avoid counting all the entries in FileTags each time you render it. Holding the count would then be a help.
But remember, writes can be expensive. So have a clear idea what you are going to do with the counts before you decide to store them. Maintaining them asynchronously, perhaps in a batched job, would be a good idea.
To find the number of times tag "ABC" occurs just do
SELECT COUNT(*) FROM FilesTags Table WHERE Tag Name = "ABC";
Hope this helps.

Stumbleupon type query

Wow, makes your head spin!
I am about to start a project, and although my mySql is OK, I can't get my head around what required for this:
I have a table of web addresses.
id,url
1,http://www.url1.com
2,http://www.url2.com
3,http://www.url3.com
4,http://www.url4.com
I have a table of users.
id,name
1,fred bloggs
2,john bloggs
3,amy bloggs
I have a table of categories.
id,name
1,science
2,tech
3,adult
4,stackoverflow
I have a table of categories the user likes as numerical ref relating to the category unique ref. For example:
user,category
1,4
1,6
1,7
1,10
2,3
2,4
3,5
.
.
.
I have a table of scores relating to each website address. When a user visits one of these sites and says they like it, it's stored like so:
url_ref,category
4,2
4,3
4,6
4,2
4,3
5,2
5,3
.
.
.
So based on the above data, URL 4 would score (in it's own right) as follows: 2=2 3=2 6=1
What I was hoping to do was pick out a random URL from over 2,000,000 records based on the current users interests.
So if the logged in user likes categories 1,2,3 then I would like to ORDER BY a score generated based on their interest.
If the logged in user likes categories 2 3 and 6 then the total score would be 5. However, if the current logged in user only like categories 2 and 6, the URL score would be 3. So the order by would be in context of the logged in users interests.
Think of stumbleupon.
I was thinking of using a set of VIEWS to help with sub queries.
I'm guessing that all 2,000,000 records will need to be looked at and based on the id of the url it will look to see what scores it has based on each selected category of the current user.
So we need to know the user ID and this gets passed into the query as a constant from the start.
Ain't got a clue!
Chris Denman
What I was hoping to do was pick out a random URL from over 2,000,000 records based on the current users interests.
This screams for predictive modeling, something you probably wouldn't be able to pull off in the database. Basically, you'd want to precalculate your score for a given interest (or more likely set of interests) / URL combination, and then query based on the precalculated values. You'd most likely be best off doing this in application code somewhere.
Since you're trying to guess whether a user will like or dislike a link based on what you know about them, Bayes seems like a good starting point (sorry for the wikipedia link, but without knowing your programming language this is probably the best place to start): Naive Bayes Classifier
edit
The basic idea here is that you continually run your precalculation process, and once you have enough data you can try to distill it to a simple formula that you can use in your query. As you collect more data, you continue to run the precalculation process and use the expanded results to refine your formula. This gets really interesting if you have the means to suggest a link, then find out whether the user liked it or not, as you can use this feedback loop really improve the prediction algorithm (have a read on machine learning, particularly genetic algorithms, for more on this)
I did this in the end:
$dbh = new NewSys::mySqlAccess("xxxxxxxxxx","xxxxxxxxxx","xxxxxxxxx","localhost");
$icat{1}='animals pets';
$icat{2}='gadget addict';
$icat{3}='games online play';
$icat{4}='painting art';
$icat{5}='graphic designer design';
$icat{6}='philosophy';
$icat{7}='strange unusual bizarre';
$icat{8}='health fitness';
$icat{9}='photography photographer';
$icat{10}='reading books';
$icat{11}='humour humor comedy comedian funny';
$icat{12}='psychology psychologist';
$icat{13}='cartoons cartoonist';
$icat{14}='internet technology';
$icat{15}='science scientist';
$icat{16}='clothing fashion';
$icat{17}='movies movie latest';
$icat{18}="\"self improvement\"";
$icat{19}='drawing art';
$icat{20}='latest band member';
$icat{21}='shop prices';
$icat{22}='recipe recipes food';
$icat{23}='mythology';
$icat{24}='holiday resorts destinations';
$icat{25}="(rude words)";
$icat{26}="www website";
$dbh->Sql("DELETE FROM precalc WHERE member = '$fdat{cred_id}'");
$dbh->Sql("SELECT * FROM prefs WHERE member = '$fdat{cred_id}'");
#chos=();
while($dbh->FetchRow()){
$cat=$dbh->Data('category');
$cats{$cat}='#';
}
foreach $cat (keys %cats){
push #chos,"\'$cat\'";
push #strings,$icat{$cat};
}
$sqll=join("\,",#chos);
$words=join(" ",#strings);
$dbh->Sql("select users.id,users.url,IFNULL((select sum(scoretot.scr) from scoretot where scoretot.id = users.id and scoretot.category IN \($sqll\)),0) as score from users WHERE MATCH (description,lasttweet) AGAINST ('$words' IN BOOLEAN MODE) AND IFNULL((SELECT ref FROM visited WHERE member = '$fdat{cred_id}' AND user = users.id LIMIT 1),0) = 0 ORDER BY score DESC limit 30");
$cnt=0;
while($dbh->FetchRow()){
$id=$dbh->Data('id');
$url=$dbh->Data('url');
$score=$dbh->Data('score');
$dbh2->Sql("INSERT INTO precalc (member,user,url,score) VALUES ('$fdat{cred_id}','$id','$url','$score')");
$cnt++;
}
I came up with this answer about three months ago, and just cannot read it. So sorry, I can't explain how it finally worked, but it managed to query 2 million websites and choose one based on the history of a users past votes on other sites.
Once I got it working, I moved on to another problem!
http://www.staggerupon.com is where it all happens!
Chris