Archive for the 'SQL' Category

26
Feb

sf_guard_user_profile id and user_id are messed up

If you use symfony’s sfDoctrineApply plugin and for some reason, the ‘id’ and ‘user_id’ in sf_guard_user_profile are messed up, you can try the below SQL.

The SQL would update sf_guard_user’s id to match the sf_guard_user_profile’s id.

Take note that this is provided that your other application tables are using sf_guard_user_profile‘s id and not the sf_guard_user‘s id.

Continue reading ‘sf_guard_user_profile id and user_id are messed up’

VN:F [1.9.7_1111]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
23
Jan

Using single SQL query to retrieve counts of two subqueries

Recently received a question from someone asking for a single SQL statement to retrieve the counts for two subqueries (or something like that).

I had altered the question and and my answer below.

A company has several customers and courses. Each customer may be signing up on several courses.
Write an SQL query to display the number of women and the number of men signing on each course.

And my simple database design

CREATE TABLE `CUSTOMER` (
`CUSTOMER_ID` INT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 100 ) NULL ,
`GENDER` CHAR( 1 ) NULL ,
`EMAIL` VARCHAR( 150 ) NULL
) ENGINE = MYISAM ;

CREATE TABLE `COURSE` (
`COURSE_ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 50 ) NOT NULL ,
`DESCRIPTION` VARCHAR( 255 ) NOT NULL ,
`START_DATE` DATE NOT NULL ,
`END_DATE` DATE NOT NULL
PRIMARY KEY ( `COURSE_ID` )
) ENGINE = MYISAM ;

CREATE TABLE `CUSTOMER_COURSE` (
`CUSTOMER_ID` INT NULL ,
`COURSE_ID` INT NULL ,
`STATUS` VARCHAR( 5 ) NULL
) ENGINE = MYISAM ;

And the SQL to “display the number of women and the number of men signing on each course”

select
    sum(total_males) as total_males,
    sum(total_females) as total_females,
    COURSE_id
from
    (select COUNT(e.GENDER) as total_males , 0 as total_females, ep.COURSE_ID
        from CUSTOMER e, CUSTOMER_COURSE ep
        where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = 'M'
        group by ep.COURSE_ID
    union all
    select 0 as total_males, COUNT(e.GENDER) as total_females ,ep.COURSE_ID
        from CUSTOMER e, CUSTOMER_COURSE ep
        where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = 'F'
    group by ep.COURSE_ID
	) t1
group by COURSE_id

Explanation:

  • the two subqueries generate 3 columns: total males, total females, course ID
  • one of the columns in the two subqueries is 0
  • the two subqueries would then UNION ALL to populate all results for the parent SQL
  • the parent SQL would then do a SUM/COUNT for the subqueries by grouping the course ID.
  • the ‘t1′ is just a empty table name thrown back to the parent for reference
VN:F [1.9.7_1111]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)



Search Website

www.lingeriesg.com

www.lingeriesg.com

Sponsored Ads

Sponsored Links

www.asiadolly.com

www.asiadolly.com

About Me

Me

Hello, Im Benny Chong. This blog is a proof that I have too much spare time to burn. 

I will be writing topics mainly on web design, application development, photography and other rubbish. Cheers