Trouble with MySQL query - mysql

OK, first-off I admit MySQL Syntax has never been my strongest point. So, here I am.
Urls :
ID Url Code
===============================================
1 http://www.google.com Abcd
2 http://www.freetemplates4u.com Efgh
3 ...
Posts :
ID Title Address
===============================================
1 Some Title 1 http://mize.it/Abcd
2 Some Title 2 http://mize.it/Efgh
3 ...
I want to create a query to fetch the following table
Title Url
=======================================================
Some Title 1 http://www.google.com
Some Title 2 http://www.freetemplates4u.com
In a few words :
Take the Url-Code pairs from Urls table
Search for http://mize.it/+Code in the Posts table (in the Address field)
Combine the final Title and Url in a result table.
I know it has something to do with joins and concatenation, but I'm definitely lost.
SIDENOTE : I don't care neither about my current database's structure, nor about performance issues. All I want is to transfer existing data, from the existing database (without having to alter it), to my new website's database (under a totally different format/structure).

You should change your DB-Design, this query will have a poor performance since mysql has to do a full tablescan.
Try adding a code column in your Posts table hat has the right value (populate it on insert/update) and add an index to Code (both tables).
Now you should be able to do.
SELECT Posts.Title, Urls.Url
FROM Posts
INNER JOIN Urls ON Post.Code = Urls.Code
Update:
If the first part of the url is always the same, this will work
SELECT Post.Title, Urls.Url
FROM Posts
INNER JOIN Urls ON Post.Adress = CONCAT('http://mize.it/', Urls.Code)

TRY
SELECT p.title,x.url
FROM Posts p
INNER JOIN ( SELECT url, CONCAT('http://mize.it/',code) AS xcode FROM Urls ) x
ON (x.xcode = p.address)
Working DEMO

This is a different approch, it took a while for me to test it.
Since your Address field contains complete url and we only need to match what is after / so we can replace actual url with nothing (I assume url is always the same) and have string ready to be matched with Code field.
SELECT b.Title, a.URL
FROM Url a
LEFT JOIN Posts b
ON a.Code = REPLACE(b.Address, 'http://mize.it/', '')
ORDER BY a.ID ASC

Check following query.
select m1.Url, m2.Title from Urls as m1, Posts as m2
where m2.address like 'http://mize.it/%'

Related

mySQL multiple results into one result row

I have a table that stores image URLs by product id and am trying to write a query that will pull all image results onto one line. The table looks something like:
photoFlag photoName objectID
where photoName holds the relative path to the image and photoFlag identifies the type of image (full, thumb, etc). I started off by writing a quick query to get two images (with different flags) to pull up. This is what I have:
select t2.pic, t4.pic, t2.id from
(select p2.photoName as pic, p2.objectID as id from ds_photos as p2 where p2.photoFlag = 2) as t2,
(select p4.photoName as pic, p4.objectID as id from ds_photos as p4 where p4.photoFlag = 4) as t4
where t2.id=t4.id
which seems to be a correct query but when I execute via phpMyAdmin it never returns a result and shows up indefinitely (well, at least for about 40 min) in the "SHOW PROCESS" list. Is there something that I have wrong that I'm not seeing in here that is causing an endless loop?
Ideally I would like grab only pics with photoFlag=2 (some products have multiple images) and put them into the same row but I have no idea where to start with that. Any suggestions?
Thanks for the help.
Use: GROUP_CONCAT:
SELECT id, GROUP_CONCAT(photoName ) As pic
FROM ds_photos
WHERE (photoFlag = 2 or photoFlag = 4) and id = ?;
GROUP BY id
replace the ? with the ID you want.

MYSQL join with sort and group by choosing the element to display

I have 2 tables, evt and content. content is link to evt throw the column content_evt_fk (to make it simpler, you can replace evt by article and content by comment for a blog database).
What I'm trying to do is to have in one query, the evt id, the evt name, the number of content related to it, and the id and the text of the last inserted content related to this evt.
here is the code :
SELECT
`evt`.`evt_id`,
`evt`.`name`,
`content`.`content_id`,
`content`.`content_text`,
count(*) as `evt_cont`
FROM
`t_evt` as `evt`,
`t_content` AS `content`
WHERE
`evt`.`evt_id` = `content`.`content_evt_fk`
group by `evt_id`
ORDER BY `content`.`content_id` DESC
The issue is that the content_text sent is not the one from the last inserted, but the one for the first inserted row.
I tried tu put some Max() in the query but it didn't helped.
Any clue ?
EDIT : data Sample
let's say I have an evt called "pocket" and 3 related content, inputed in this order ("1-cake","2-chocolate","3-sweets").
The result of my query is
evt_id name content_id content_text evt_cont
149 pocket 112 1-cake 3
What I would like to have :
evt_id name content_id content_text evt_cont
149 pocket 115 3-sweets 3
I'm not working with MySql so there are probably better ways to do this. The simplest way is to join derived table of max (column-representing-order-of-interest, content_id here) by key (content_evt_fk here) to original table filtering out all but last entries.
SELECT
evt.evt_id,
evt.name,
content.content_id,
content.content_text
FROM t_evt as evt
INNER JOIN t_content AS content
ON evt.evt_id = content.content_evt_fk
INNER JOIN
(
select content_evt_fk, max(t_content.content_id) content_id
from t_content
group by content_evt_fk
) last_content
ON content.content_id = last_content.content_id
-- This is not necessary here, but if you do this sort of thing on
-- columns other than id, you will have to join this part too
and content.content_evt_fk = last_content.content_evt_fk

Mysql: pulling from table

I am having some trouble putting a query together. I need to show images pulled in the order of if they are in the "editorial" section then if they have an order to be displayed in it will show the editorial image first but if its not ordered in that section it would just default and pull the regular image that is ordered already (which may not be a editorial type image but is a preferred one if nothing else is available). What I have now is the query below BUT that doesn't pull the editorial ranked images first but rather the "ordered_by' seems to take precedence.
SELECT i.img_name, a.artist_path_name, a.artist_dir, a.artist_name, ck.catKey_id
FROM images AS i JOIN artists AS a USING (artist_id)
JOIN img_cat_table AS imc USING ( img_id )
JOIN catkeys AS ck USING (catKey_id)
WHERE site = 'editorial' AND editorial_order = 1 OR ordered_by = 1 GROUP BY artist_name ORDER BY ed_banner
Its probably something silly that I am missing -- any and all help is appreciated.
Try something like:
...
ORDER BY CASE WHEN site = 'editorial' AND editorial_order = 1 THEN 1 ELSE 2 END,
ed_banner
or the same idea in a simpler way
ORDER BY (site = 'editorial' AND editorial_order = 1) DESC, ed_banner
It simply utilizes the order of FALSE, TRUE.
You should remove the respective conditions from the WHERE clause.

MediaWiki 1.16.0 - In phpmyadmin select the current articles

I'm trying to get all articles that are the current/latest articles in Mediawiki 1.16.0. I need to do this in phpMyadmin and make a dump from those results.
my SQL:
SELECT
page.page_title, page.page_latest
, revision.rev_id, revision.rev_text_id
, text.old_id, text.old_text
FROM page, revision, text
WHERE rev_id = page_latest AND rev_text_id = old_id
I get the image names also but not a problem. I feel that this SQL above is not getting the latest version of the articles.
If there is a way to not show image names and redirects in the results it would also help.
First of all please don't use that ugly implicit join syntax. It's confusing and error-prone.
Change it to this:
SELECT
page.page_title, page.page_latest
, revision.rev_id, revision.rev_text_id
, text.old_id, text.old_text
FROM page
INNER JOIN revision ON (rev_id = page_latest)
INNER JOIN text ON (rev_text_id = old_id)
Now you can see why: it's getting all pages. There is no where clause, there are just join clauses.
This is the DB layout: http://upload.wikimedia.org/wikipedia/commons/b/b7/MediaWiki_database_schema_1-17_%28r82044%29.png
And here are the description of the fields in the various tables:
http://www.mediawiki.org/wiki/Manual:Database_layout
Revised query
SELECT
p.page_title, p.page_latest
, MAX(revision.rev_id) as rev_id, revision.rev_text_id
, text.old_id, text.old_text
FROM page p
INNER JOIN revision r ON (r.rev_id = p.page_latest)
INNER JOIN `text` t ON (r.rev_text_id = t.old_id)
WHERE p.page_is_redirect = 0 AND p.page_namespace <> 6 /*NS_IMAGE = 6*/
GROUP BY p.page_latest
ORDER BY p.page_title
This filters out the redirects and excludes the pages where namespace = ns_image.
I'm not 100% sure though 'cause I don't have MediaWiki to test.

Custom Order By + Group By

So I have a manual in this table:
id lang header_name text
1 uk Youth Development It's very important
2 dk tst hejsa
3 uk tst hello sir
And I want to make a query that fetches all manual entries for a given language (danish in this case). If for some reason not all 100% of the original manual entries (the UK ones), has been translated I want to get the english entry instead. Is that even possible in table formats such as this?
I guess it would be something with a "group by header_name" of some sorts, but not sure.
Try this, i dont have an SQL and hence this is not tested
The tables t1, t2, t3 refer to the same table use an alias to distinguish them;
select * from t3
where t3.lang IN ('DK','UK')
and t3.ID NOT IN
(select t1.id
FROM t1,t2
where t1.header_name = t2.header_name
AND t2.lang = 'DK'
AND t1.lang = 'UK'
)
Essentially first you need to find the ID that have translation, and then exclude them.
This might do the trick but it is not optimized:
SELECT *
FROM the_table
WHERE lang = 'dk'
UNION
SELECT *
FROM the_table
WHERE lang <> 'dk' AND header_name NOT IN (
SELECT header_name
FROM the_table
WHERE lang = 'dk'
)
I cant comment but all i want to ask is:
In the example you just put up, the rows with Id 2 and ID 3 are the same entries only different language?
Id say you pull it out and make two tables
Example
id
sort
(all other generic columns)
example_translations
id
example_id
language_id
header_name
text
Then if querying for the danish translation of an example with id 1 it'll return the example_translations row of this entity. if it returns nothing you can query for the english version.
I dont think it is possible to do something like this on Mysql level
The way i understand this, you want to get the english content if the danish content is missing?.. You might want to add a column to your table where you mark your entries. (dont know if your "header_name" column does that efectly for you, i'm guessing that as well will be translated?..
Anyway, a column named "entry_id" where "tst dk" and "tst uk" would both have id "2" for an example, you should then when you load you manual ask for the "entry_id" and first look for the dk entry, and if it's not there, load the uk entry.