Jump to content
php.lv forumi

Selects iekš selecta,


Sasa

Recommended Posts

Atbilde vēl aizvien nav viennozīmīga :)

Abās - nozīmē, ka gan vienā, gan otrā? Vai abās nozīmē vismaz vienā?

Līdz ar to vai nu:

SELECT *
FROM product
WHERE
id IN (
SELECT product_id FROM accessories
WHERE accessories.product_id = product.id
)
AND
id IN (
SELECT product_id FROM details
WHERE details.product_id = product.id
)

vai arī:

SELECT *
FROM product
WHERE
id IN (
SELECT product_id FROM accessories
WHERE accessories.product_id = product.id
)
OR
id IN (
SELECT product_id FROM details
WHERE details.product_id = product.id
)

Link to comment
Share on other sites

man tas produkta ID jāsastop gan accessories tabulā, gan details tabulā, un tikai tad es varu pildīt darbības tālāk.

Izdomāju šo fīču tamdēļ, lai mans produktu list's aizpildās tikai ar tiem produktiem kuriem ir specene pievienota.

Link to comment
Share on other sites

Tātad, ja man ir tabula

product:

1

2

3

4

 

Tabula

accessories:

1

2

3

 

Tabula

details:

2

3

4

 

Tu gribi, lai tas selekts izvada:

2

3

 

Vai ne?

 

Faktiski derēja arī tavs pirmais vaicājums - vienkārši vajadzēja, lai izvada tikai vienu lauku tas subselects (un pilnīgi vienalga, vai details, vai accessories id tiktu izvadīts):

SELECT *
FROM product
WHERE ID
IN (

SELECT accessories.product_id
FROM accessories, details
WHERE accessories.product_id = details.product_id
)

Link to comment
Share on other sites

jā, lai vai kā

šis querijs nostrādāja:

SELECT *
FROM product
WHERE
id IN (
SELECT product_id FROM accessories
WHERE accessories.product_id = product.id
)
AND
id IN (
SELECT product_id FROM details
WHERE details.product_id = product.id
)

 

paldies,

Link to comment
Share on other sites

izpēti tikai ar EXPLAIN, kurš no abiem ir labāks:

SELECT *
FROM product
WHERE
id IN (
SELECT product_id FROM accessories
WHERE accessories.product_id = product.id
)
AND
id IN (
SELECT product_id FROM details
WHERE details.product_id = product.id
)

vai šitas (man aizdomas, ka labāks):

SELECT *
FROM product
WHERE ID
IN (
SELECT accessories.product_id
FROM accessories, details
WHERE accessories.product_id = details.product_id
)

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.

Loading...
×
×
  • Create New...