0
SELECT `p2`, COUNT(*) FROM dsf_2022_raw_new WHERE `p2` LIKE 'BA' 

p2   COUNT(*)
BA    608

I was able to get count from that table but I need to get "BA" from 4 more tables with similar structure.

Then what if I want to select BA, DL, TS, AL these are actually first two characters of the post code.

3
  • 1
    Read about group by and union Commented Oct 5, 2022 at 8:31
  • You can use OR in WHERE Commented Oct 5, 2022 at 8:32
  • UNION ALL the tables. Or, if you really need separate tables, create a view that does UNION ALL. Commented Oct 5, 2022 at 8:36

1 Answer 1

1

welcome to Stack Overflow. So, looks like you're asking two questions there.

  1. Regarding getting the count from four separate tables, I suggest running the same SQL query you've written on these four tables, saving each query output to a single view, and then running a group-by query to calculate the total count. Something like
SELECT count(*), p2 from <your_view> group by p2
  1. Selecting queries from multiple postcodes can be achieved using OR statements with your where clause. So, taking your original query,
SELECT p2, COUNT(*) FROM dsf_2022_raw_new 
WHERE p2 LIKE 'BA' 
OR WHERE p2 LIKE 'DL'... 

Hope this helps. Happy coding

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.