简体   繁体   中英

i only get the last record from mysql query

it's a simple matter, but i don;t get it

i have two tables in a DB date and newsmail in date i have some columns ID, name, address, email, etc in newsmail i have 2 fields ID, mail

i need to extract all records from date, that contains emails addresses from newsmail

i've tried :

SELECT * FROM date WHERE date.email IN (SELECT mail FROM newsmail);

i receive only one record from date with the last row from newsmail also

SELECT * FROM date d JOIN newsmail n ON d.email LIKE n.mail

same result as above... email and mail fields are VARCHAR(50)

http://sqlfiddle.com/#!2/fad06/1/0 here it's working, but not on my real DB

the problem was at inserting data to newsmail from txt file

INITIAL :

$tmpfisier = $_FILES["upfile"]["tmp_name"];
$file_handle = fopen($tmpfisier, "r");
    while(!feof($file_handle)){
        $email_line = fgets($file_handle);
        $values = mysql_query("INSERT INTO  newsmail (`ID` ,`mail`) VALUES ( NULL ,  '$email_line')");
    }
    fclose($file_handle);

EDITED :

$tmpfisier = $_FILES["upfile"]["tmp_name"];
$file_handle = fopen($tmpfisier, "r");
    while(!feof($file_handle)){
        $raw_email_line = fgets($file_handle);
        $email_line = preg_replace('/\s\v/u', '', $raw_email_line);
        $values = mysql_query("INSERT INTO  newsmail (`ID` ,`mail`) VALUES ( NULL ,  '$email_line')");
    }
    fclose($file_handle);

so the problem was WHITESPACES from txt/csv file

THANK YOU ALL !!!

SELECT  a.*
FROM    Date a
        INNER JOIN NewsMail b
            ON a.email LIKE CONCAT('%', b.mail, '%')

Use this:

SELECT d.*
FROM date d
INNER JOIN newsmail n
    ON n.mail = d.email;

The TRIM function may be needed to remove leading and trailing spaces.

SELECT * FROM date d LEFT JOIN newsmail n ON TRIM(d.email) = TRIM(n.mail)

For SQLSERVER, you may need to use the LTRIM and RTRIM together to get the same results.

SELECT * FROM date d LEFT JOIN newsmail n ON RTRIM(LTRIM(d.email)) = RTRIM(LTRIM(n.mail))

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