简体   繁体   中英

Query producing incorrect results

Our query is designed to pull two pieces of content from two separate tables and join them. however, upon displaying the information on a web page, it seems that it is only pulling content from one table, or pulling information from two tables and then also displaying info from another table that shouldn't have been specified because only two tables should be accessed by our query. Any ideas whats wrong? Heres the code:

  randnum = random.randint(1,4)                                                                                                                              
  randnum2 = random.randint(1,4)                                                                                                                             
  trivia == 'Combo-Trivia!'                                                                                                                                  
  if randnum == 1:                                                                                                                                           
    tcol = 'laws'                                                                                                                                            
  elif randnum == 2:                                                                                                                                         
    tcol = 'trivia'                                                                                                                                          
  elif randnum == 3:                                                                                                                                         
    tcol = 'sayings'                                                                                                                                         
  else:                                                                                                                                                      
    tcol = 'fortuneCookies'                                                                                                                                  
  if randnum2 == 1:                                                                                                                                          
    tcol2 = 'laws'                                                                                                                                           
  elif randnum2 == 2:                                                                                                                                        
    tcol2 = 'trivia'                                                                                                                                         
  elif randnum2 == 3:                                                                                                                                        
    tcol2 = 'sayings'                                                                                                                                        
  else:                                                                                                                                                      
    tcol2 = 'fortuneCookies'                                                                                                                                 
  cur.execute('select ' +tcol +'.content,'+ tcol2 +'.content from '+tcol+' JOIN '+tcol2+' order by rand() limit 1')                                          
  rows = cur.fetchall()                                                                                                                                      
  print rows                                                                                                                                                 
  return render_template('mashdisplay.html', trivia = trivia, rows = rows) 

I realize that there is a chance it will try to sort the same table twice, but the times that it doesn't give that error it either presents content from only one table, or presents content from 3 tables.

Using python and Flask with MySQL for this. Any and all help is appreciated!

edit: What we're trying to do is take infromation from two random tables within a database, and have the query produce two pieces of content (content is a column defined within each table) which can then be displayed on a web page which this information is directed to. For example: content is selected from the law table, and content is selected from the sayings table, the two are joined, and then the output of the joining is displayed on the webpage. I recognize that we could do two separate queries for this however having an individual query is preferable.

It's not at all clear what you are doing here. You don't specify any criteria for the tables to join on, so what you end up with is a cartesian join : ie every row of tcol1 joined to every row of tcol2. That's undoubtedly not what you want, but I don't understand what you do want so I can't help you fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM