0

My goal is to try to find the total count of rows (table1) and the total count of those rows that have been translated (translation is in table2). I have this query that works and gives me the count of the things I want.

SELECT COUNT(a.text) 
FROM table1 a
LEFT JOIN table2 b ON a.id = b.id

It just seems redundant to write the query again when I know I can just change the parameter within the COUNT() to b.text.

Is there a way to pass in a parameter within the COUNT() like this COUNT(@Parameter) and maybe do a foreach loop? FYI I'm working with webforms.. Thank you in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rlatmfrl
  • 17
  • 4
  • 1
    you can use "normal" string concatenation https://stackoverflow.com/a/7441372/5193536 – nbk Oct 03 '22 at 22:27
  • The SQL you've posted does not use parameters. It should, which would make what you want to do extremely simple. Search this site for *C# SQL parameterized queries* - there are many existing examples of writing C# code that uses them on this site. The first result of that search turned up https://stackoverflow.com/q/32752525/62576 – Ken White Oct 04 '22 at 00:08
  • 1
    @nbk: Using string concatenation for SQL is a horrible idea, as it allows for mistakes and SQL injection, and makes it more difficult to properly quote values and use proper data types. Advising anyone (especially a new programmer) to do so is irresponsible - we try to share *good* knowledge here, not bad. – Ken White Oct 04 '22 at 00:09
  • @KenWhite yes, but he can use a whitelist for all column names, so it will be secure – nbk Oct 04 '22 at 00:11
  • @nbk: That's nonsense. String concatenation of SQL is horribly unsecure and dangerous, and new programmers should learn the **proper way** to write code from the beginning, and teaching them otherwise is totally working against the goals of this site. Turn your comment into an actual answer and see how many dozens of downvotes you can collect very quickly. We don't teach bad coding habits here, and you shouldn't be in comments either. – Ken White Oct 04 '22 at 00:13
  • @KenWhite i don't know where you come from but a whitelist for column names or table names is a common tools to secure queries against sql injection, if you need dynamic qzueries, there are more more complex ones like checking against the database, but for an handfull of names this is the right approach – nbk Oct 04 '22 at 00:26

1 Answers1

0

No sure, but you can try this

SELECT COUNT(*), COUNT(b.text)  
FROM table1 a
LEFT JOIN table2 b ON a.id = b.id
Pivazi Z.
  • 76
  • 5