简体   繁体   中英

SQL Query result, comparison and where clause

I am building a site and i need to retrieve some information. I have this query.

$SQL = "SELECT distretto_108, provinca_113, regioni_116, tipologia_pdv_106, 
        richiesta_ccnl_107, coop_va_109, nome_pdv_110, 
        indirizzo_pdv_111, localita_112 
        FROM civicrm_value_informazioni_su_tute_le_schede_p_22 ";  

I need to add this other code:

WHERE civicrm_event.title_en_US='".addslashes($_GET["titles"])."' 

but it's not working... i need to compare let's say the id of another table with the id of the current table... How to do that?

Thanks in advance...

You should learn something about joining tables...

Do not know what the relation is between the two tables (simply said: what column from one table is pointing to what column at other one), but try something similar (modification needed to meet You DB structure) - now lets assume both tables have related column called event_id:

$SQL = "SELECT distretto_108, provinca_113, regioni_116, tipologia_pdv_106, 
        richiesta_ccnl_107, coop_va_109, nome_pdv_110, 
        indirizzo_pdv_111, localita_112 
    FROM civicrm_value_informazioni_su_tute_le_schede_p_22 cvistlsp22
    LEFT JOIN civicrm_event ce ON ce.event_id = cvistlsp22.event_id
    WHERE ce.title_en_US='".mysql_real_escape_string($_GET["titles"])."'";

civicrm_value_informazioni_su_tute_le_schede_p_22 table name is very long and You will not be able to create a table with such long name in other DBMS (eg ORACLE), so try to make it shorter while still self-describing...

If You want to join tables they have to have a relation, read more about relations and how to use them here: http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/

You are retrieving the data from table civicrm_value_informazioni_su_tute_le_schede_p_22 in your query while the where clause you are adding, refers to the table civicrm_event. You need to add this new table in the from clause and do a join among the two tables using some common key. Example below:

$SQL = "
SELECT distretto_108, provinca_113, regioni_116, tipologia_pdv_106, richiesta_ccnl_107, coop_va_109, nome_pdv_110, indirizzo_pdv_111, localita_112 
FROM civicrm_value_informazioni_su_tute_le_schede_p_22 
  JOIN civicrm_event ON civicrm_value_informazioni_su_tute_le_schede_p_22.ID_PK = civicrm_event.ID_FK
WHERE civicrm_event.title_en_US='".addslashes($_GET["titles"])
"; 

You need to replace the ID_PK and ID_FK with the relevant Primary and Foreign Keys that bind the tables together.

Please note using query params like that is not recommended. Please read PHP Documentation here for more explanation.

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