Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

how to count primary key of one table which is used in another table in 5 to 6 column?

If table 1 has primary key srno which is used in table2 in column d1,d2,d3,d4.

I want to count how many times srno=1,2,3,4... etc used in table2.

any one know how to do that???

share|improve this question

closed as off-topic by ypercube, Aaron Bertrand Jun 27 '13 at 17:59

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Edit your question and add the CREATE TABLE statements, a few rows with sample data and what you want as output. If you have a query you tried, add that, too. You can also use SQL-Fiddle and add here a link to the page you created. –  ypercube May 28 '13 at 12:51
    
If the same srno value is used multiple times in a single row, does that count as 1, or does it count as the number of times it appears? Also, which RDBMS? –  Jon Seigel May 28 '13 at 14:14
    
And what php has to do with this question? –  dezso Jun 27 '13 at 16:43
1  
This question appears to be off-topic because it was cross-posted at SO: stackoverflow.com/questions/16789024/… –  ypercube Jun 27 '13 at 17:05
add comment

1 Answer 1

Total of all columns regardless of duplicates.

SELECT count(d1) + count(d2) + count(d3) + count(d4) FROM Table2;

Total of each column regardless of duplicates.

SELECT count(d1), count(d2), count(d3), count(d4) FROM Table2;

Total of each column without duplicates.

SELECT 
     count(distinct d1)
   , count(distinct d2)
   , count(distinct d3)
   , count(distinct d4) 
FROM Table2;

Total of all columns without duplicates.

SELECT count(distinct d1) FROM
(
  SELECT d1 FROM Table2
  UNION 
  SELECT d2 FROM Table2
  UNION 
  SELECT d3 FROM Table2
  UNION 
  SELECT d4 FROM Table2
);

SQLFiddle

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.