Jump to content
susp3kt

Problema query

Recommended Posts

Buna ziua,

Am si eu o problema cu selectarea unor date dintr-un tabel pe baza conditiilor din alt tabel.

tabel 1 "telefoane"

id | hash | telefon

tabel 2 "log_telefoane"

id | hash | id_produs

$id_produs = 1;

Cum extrag din tabelul 1 (telefoane) campurile in care hash != cu hash din tabel 2 sau daca exista hash-ul in tabelul 2 sa afisez doar daca id_produs != $id_produs

Am incercat cu urmatorul query insa nu functioneaza corect


$this->db->query("SELECT t1.* FROM telefoane t1 LEFT OUTER JOIN log_telefoane t2 ON t2.hash = t1.hash WHERE t1.hash IS NULL OR t2.id_produs != 1");

Va multumesc

Link to comment
Share on other sites

Nu chiar, dar iti multumesc pentru incercare.

Uite aici, am mai simplificat putin: Error 404 Not Found

Ideea este ca daca totusi hash-ul din tabelul telefoane exista si in log_telefoane sa mai faca o verificare in functie de campul produs si daca produsul != "Alcatel" sa il afiseze.

Ceva de genul, dar nu este scris corect si


SELECT
*
FROM
telefoane
WHERE
hash NOT IN (SELECT hash FROM log_telefoane)
OR log_telefoane.produs != 'Alcatel'

Link to comment
Share on other sites

Nu ai specificat corect structura tabelelor. Ai o cheie de legatura intre ele? (un foreign key).

In primul post ai zis id_produs != $id_produs, apoi produs != "Alcatel". Sa inteleg ca id_produs este coloana "telefon" din primul tabel? In tabelul al doilea si si produs si id_produs?

Edited by Wav3
Link to comment
Share on other sites

Nu ai specificat corect structura tabelelor. Ai o cheie de legatura intre ele? (un foreign key).

In primul post ai zis id_produs != $id_produs, apoi produs != "Alcatel". Sa inteleg ca id_produs este coloana "telefon" din primul tabel? In tabelul al doilea si si produs si id_produs?

Salut,

Scuze de intarziere.

Uite un exemplu mai usor de inteles:

Avem 2 tabele.

Tabel 1: telefoane

Tabel 2: log_telefoane

Tabel 1 are 2 campuri: id si telefon

Tabel 2 are la fel, tot id si telefon

In tabel1 am introduse 10 numere de telefon, iar 3 dintre acestea sunt si in tabelul log_telefone

Cum fac sa selectez din tabel1 un numar, dar cu conditia ca acesta sa nu existe in tabelul2?

Va multumesc

Edited by susp3kt
Link to comment
Share on other sites

Salut,

Scuze de intarziere.

Uite un exemplu mai usor de inteles:

Avem 2 tabele.

Tabel 1: telefoane

Tabel 2: log_telefoane

Tabel 1 are 2 campuri: id si telefon

Tabel 2 are la fel, tot id si telefon

In tabel1 am introduse 10 numere de telefon, iar 3 dintre acestea sunt si in tabelul log_telefone

Cum fac sa selectez din tabel1 un numar, dar cu conditia ca acesta sa nu existe in tabelul2?

Va multumesc

Asa zi, frate.

SELECT *

FROM telefoane

LEFT JOIN log_telefoane ON (log_telefoane.telefon = telefoane.telefon)

WHERE log_telefoane.telefon IS NULL

  • Downvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...