2017-06-26 2 views
0

Ich möchte alle Tabellen beitreten, in allen Tabellen sind alle Spaltennamen gleich. Meine Frage ist unten. Bitte helfen Sie mir, die Daten zu holen.
Meine Tabellenstruktur CREATE TABLE IF NOT play_school ( token_id varchar (255) NOT NULL, id int (11) NOT NULL AUTO_INCREMENT VORHANDEN, ad_id varchar (255) NOT NULL, title Text NOT NULL, category varchar (255) NOT NULL, name Text NOT NULL, image varchar (255) NOT NULL, content Text NOT NULL, offer Text NOT NULL, note Text NOT NULL, price varchar (255) NOT NULL, address varchar (255) NOT NULL, contact_no Text NOT NULL, email_id Text NOT NULL, timestamp Datum NOT NULL, status varchar (255) NOT NULL, PRIMARY KEY (id), UNIQUE KEY token_id (token_id), UNIQUE KEY token_id_2 (token_id) ) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 49;Tabelle Join in PHP mysql

Alle Tabellen Struktur sind gleich für car_showroom, Coaching, elektronische .... wie play_school Tabelle, in allen Tabellen ad_id = 'xyz' sind die gleichen und die get-ID ist auch 'xyz'.now ich möchte Daten abrufen Alle Tabellen mit ad_id = 'xyz'. Diese Abfrage wird ohne Fehler ausgeführt. aber die Daten nicht abgeholt.

$email_id = $_GET['id']; 

$result = $con->prepare("SELECT * FROM bike_showroom 
    JOIN car_showroom 
    JOIN coaching 
    JOIN college 
    JOIN electronic 
    JOIN furniture_showroom 
    JOIN hospital 
    JOIN job 
    JOIN mobile_shop 
    JOIN pets_shops 
    JOIN play_school 
    JOIN real_estate 
    JOIN services 
    JOIN shopping_store 
    JOIN stationary_shops 
    JOIN sweet_shop 
    WHERE bike_showroom.ad_id = '".$email_id. 
     "' AND car_showroom.ad_id = '".$email_id. 
     "' AND coaching.ad_id = '".$email_id. 
     "' AND college.ad_id = '".$email_id. 
     "' AND electronic.ad_id = '".$email_id. 
     "' AND furniture_showroom.ad_id = '".$email_id. 
     "' AND hospital.ad_id = '".$email_id. 
     "' AND job.ad_id = '".$email_id. 
     "' AND mobile_shop.ad_id = '".$email_id. 
     "' AND pets_shops.ad_id = '".$email_id. 
     "' AND play_school.ad_id = '".$email_id. 
     "' AND real_estate.ad_id = '".$email_id. 
     "' AND services.ad_id = '".$email_id. 
     "' AND shopping_store.ad_id = '".$email_id. 
     "' AND stationary_shops.ad_id = '".$email_id. 
     "' AND sweet_shop.ad_id = '".$email_id."' "); 

$result->execute(); 
$row = $result->fetch(); 
for($i=0; $row = $result->fetch(); $i++){ 
    echo $row['title']; 
} 
+0

Sie müssen eine gemeinsame Beziehung zwischen den Tabellen und JOIN auf denen finden. Es funktioniert auch in diesem Fall nicht, es sei denn, es gibt eine Zeile in jeder Tabelle mit dieser $ email_id. Sie müssen wahrscheinlich Ihren Ansatz überdenken. Im Allgemeinen möchten Sie nicht mehr als ein paar Tische gleichzeitig verbinden. – Cfreak

+0

Nun, funktioniert es? Fast funktioniert? Überhaupt nicht funktioniert? Gibt es Fehlerprotokolle? Was ist die erwartete Ausgabe? Wie können wir helfen, wenn wir nicht wissen, was das Problem ist? – crizzis

+1

Auch Ihre Abfrage ist anfällig für SQL-Injection. Sie möchten das definitiv beheben. Sie können darüber lesen, was es ist und wie es hier zu beheben ist: http://bobby-tables.com/ – Cfreak

Antwort

0
1. If you want to JOIN tables , you need to JOIN them ON common fields. 

2. The WHERE clause that you are using is not a JOIN parameter, but a FILTER. 

3. You cannot possibly design a database where you JOIN one table with 20 more tables. 

4. An example for you to understand. You have 2 tables, orders and orders_items 

    table each order may contain several items that is why you need a second table called order_items. This table will have a column called order_id which will link to its parent table (ORDERS) and ON its id field. 

You asked help to join the tables. I have given you the understanding you need. You are not showing us your tables, so we cannot possibly do the JOINS for you. This is the best you can get for your question. Lucky you 
have not been downvoted. 
+0

Danke für die Antwort. –

0

Sie müssen UNION Klausel verwenden.

$email_id = $_GET['id']; 
$result = $con->prepare("SELECT * FROM (
    SELECT * FROM bike_showroom 
    UNION ALL 
    SELECT * FROM car_showroom 
    UNION ALL 
    SELECT * FROM coaching 
    UNION ALL 
    SELECT * FROM college 
    UNION ALL 
    SELECT * FROM electronic 
    UNION ALL 
    SELECT * FROM furniture_showroom 
    UNION ALL 
    SELECT * FROM hospital 
    UNION ALL 
    SELECT * FROM job 
    UNION ALL 
    SELECT * FROM mobile_shop 
    UNION ALL 
    SELECT * FROM pets_shops 
    UNION ALL 
    SELECT * FROM real_estate 
    UNION ALL 
    SELECT * FROM services 
    UNION ALL 
    SELECT * FROM shopping_store 
    UNION ALL 
    SELECT * FROM stationary_shops 
    UNION ALL 
    SELECT * FROM sweet_shop) as t WHERE ad_id = '".$email_id."'"); 

$result->execute(); 
$row = $result->fetch(); 
for($i=0; $row = $result->fetch(); $i++){ 
    echo $row['title']; 
}