How do I do this?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

How do I do this?

Postby wvoyance » Thu Jun 21, 2012 4:40 am

my database has a table

PID language ....
1 1 ....
1 2 ......
1 3 ....
2 1 ......
2 2 ...........
etc

i.e. each PID SHOULD have language 1,2,3
The problem is some PID have some language missing i.e. only 1,3 or 1,2 etc.
I want to find out those PID with language missing.
How should I write the SQL?
User avatar
wvoyance
Forum Contributor
 
Posts: 134
Joined: Tue Apr 17, 2012 8:24 pm

Re: How do I do this?

Postby mikosiko » Thu Jun 21, 2012 6:31 am

one easy alternative is using GROUP_CONCAT() comparing the result with whatever string you like.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
mikosiko
Forum Regular
 
Posts: 750
Joined: Wed Jan 13, 2010 8:22 pm

Re: How do I do this?

Postby wvoyance » Thu Jun 21, 2012 10:42 am

mikosiko wrote:one easy alternative is using GROUP_CONCAT() comparing the result with whatever string you like.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat



oh...I forget to say...I do not know what is exactly PID is.
They are assigned by the system (for each book), and there are holes between them, i.e.
3, 4, 6, 11, 12, ...., 667, 668, 671,...
I mean, I know at best their range, i.e. starting from 1 to some large number.
I know they are integer, countable, and positive :P

The problem is my database is somewhat inconsistent now.
Every book (PID) should have three (1,2,3) languages. But apparently some are missing.
I need to find out which one is missing.
User avatar
wvoyance
Forum Contributor
 
Posts: 134
Joined: Tue Apr 17, 2012 8:24 pm

Re: How do I do this?

Postby VladSun » Thu Jun 21, 2012 4:12 pm

Common solution to find non successive numeric "holes" (though it would be just the first element) in series:
Syntax: [ Download ] [ Hide ]
SELECT
  `table`.`field` + 1 AS `missing_field`
FROM
  `table`
LEFT JOIN
  `table` AS `missing_field_table` ON
      `table`.`fielld` + 1 = `missing_field_table`
WHERE
  `missing_field_table`.`field` IS NULL
 


PostgreSql has a set returning function called generate_series which is very useful in such cases - you can try implement it in MySQL and use it in a similar way as the one show above.
User avatar
VladSun
DevNet Master
 
Posts: 4294
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How do I do this?

Postby wvoyance » Thu Jun 21, 2012 6:05 pm

VladSun wrote:Common solution to find non successive numeric "holes" (though it would be just the first element) in series:
Syntax: [ Download ] [ Hide ]
SELECT
  `table`.`field` + 1 AS `missing_field`
FROM
  `table`
LEFT JOIN
  `table` AS `missing_field_table` ON
      `table`.`fielld` + 1 = `missing_field_table`
WHERE
  `missing_field_table`.`field` IS NULL
 


PostgreSql has a set returning function called generate_series which is very useful in such cases - you can try implement it in MySQL and use it in a similar way as the one show above.


Thank for this nice method. But this does not solve my problem.
I want to find out which PID is incomplete, i.e. do not have three language tuples 1,2,3.

I have done that by using

SELECT pid, COUNT(language) FROM products_description GROUP BY pid;
User avatar
wvoyance
Forum Contributor
 
Posts: 134
Joined: Tue Apr 17, 2012 8:24 pm

Re: How do I do this?

Postby mikosiko » Thu Jun 21, 2012 9:43 pm

wvoyance wrote:......I want to find out which PID is incomplete, i.e. do not have three language tuples 1,2,3.


as I said.... according to the objective that you are describing GROUP_CONCAT() solve your problem

Syntax: [ Download ] [ Hide ]
SELECT pid,
       GROUP_CONCAT(language ORDER BY language) AS the_lang
  FROM products_description
  GROUP BY pid
  HAVING the_lang != '1,2,3';
mikosiko
Forum Regular
 
Posts: 750
Joined: Wed Jan 13, 2010 8:22 pm

Re: How do I do this?

Postby wvoyance » Fri Jun 22, 2012 5:40 am

Thanks, learnt one more method. Which one will be quicker?
User avatar
wvoyance
Forum Contributor
 
Posts: 134
Joined: Tue Apr 17, 2012 8:24 pm


Return to Databases

Who is online

Users browsing this forum: erdlertoby and 1 guest