title: Dynamically querying database using WHERE clause
body: <p>I want to query a table dynamically to set up my code to work autonomously. in this instance the best method to use is the <code>WHERE</code> clause. so that is what I've done. I've created a function in my database but for it to work on different data in the database. I need to pass a parameter which would query that specific data that I need queried. Now the problem that I have is that I don't want my user to pass this parameter but rather I want to pass this parameter through my code but so far I just can't figure out a way to do it.</p>
  <pre><code>CREATE OR REPLACE FUNCTION comm_profile(p_artist VARCHAR(60)) 
      RETURNS BIGINT
  AS 
  $$
  
  DECLARE
  
  total       BIGINT;
  
  BEGIN
      
      WITH temp AS (
          SELECT artist, plays FROM machine_spotify WHERE artist = p_artist
          UNION ALL
          (SELECT artist, plays FROM machine_tidal WHERE artist = p_artist)
          UNION ALL
          (SELECT artist, plays FROM machine_soundcloud WHERE artist = p_artist)
          UNION ALL
          (SELECT artist, plays FROM machine_youtube WHERE artist = p_artist)
          UNION ALL
          (SELECT artist, plays FROM machine_youtube_music WHERE artist = p_artist)
          )
          SELECT SUM(plays) FROM temp INTO total;
          RETURN total;
  
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  <p>Example</p>
  <pre><code>SELECT artist, plays FROM machine_spotify WHERE artist = p_artist
  
  comm_profile('Drake')
  
  
  machine_spotify table
  | artist | plays |
  |:-----  |:----- |
  | Drake  | 161371|
  | Drake  | 273973|
  
  Output
  | comm_profile |
  |:------------ |
  |  435344      |
  </code></pre>
  <pre><code>auth_user table
  
  | id | username    |      artist     |
  |:---|:--------    |:--------------- |
  |  1 | J.cole12    |      J.cole     |
  |  2 | Drake01     |      Drake      |
  |  3 | cassper59   | casspernyovest  |
  |  4 | Kendricklmr | Kendrick Lamar  |
  
  SELECT artist FROM auth_user
  
  Output
  |      artist    |
  |:-------------- |
  |      J.cole    |
  |      Drake     |
  | casspernyovest |
  | Kendrick Lamar | 
  
  </code></pre>
  <p>I don't want this I want it to return a singular artist but I cant think of a query that would allow that without requiring a parameter</p>
  <p>now what I want to do is to create another function that will give me the <code>Drake</code> but I can't quite pin down the method. selecting the column <code>artist</code> brings back the whole column as oppose to how I want them. Which is 1 by 1. creating a while loop does the same. how do I solve this?</p>