MySQL Query between 2 tables and find not null - mysql

I have 2 tables, one for the articles and one for some attributes
I have some products that have one attribute with value Yes. I also have other attributes.
The query that i want to create is to get all products that don't have the Attribute 7.
You can check the mysql tables.
http://sqlfiddle.com/#!2/f75eec/1

select a.ArticleID,a.ArticleTitle,aa.ArticleID,aa.AttrID,aa.StringValue
from cms_articles a
inner join cms_attr_art aa on aa.ArticleID = a.ArticleID
where a.ArticleID NOT IN (select ArticleID from cms_attr_art where AttrID = 7);

You can use this query
SELECT a.ArticleId, b.attrId
FROM cms_articles a
INNER JOIN cms_attr_art b
ON a.articleId = b.articleId
WHERE NOT EXISTS (SELECT 1 FROM cms_attr_art c WHERE c.articleid = a.articleid AND c.attrId = 7)

Related

SQL query to select from two tables and return if a value exists

i have two tables:
callcosts
callcosts_custom
is there a way i can select from both tables and return the value of cost from callcosts_custom if the row exists and if it doesnt exist, return the column price from callcosts
with the following WHERE clause:
WHERE callcosts_custom.parent = callcosts.sequence
basically, the callcosts table has all of the default data in and custom data can be added per customer.
the relationship is as follows:
call_costs.sequence = callcosts_custom.parent
so i want to check if a row exists in callcosts_custom for a specific callcosts_custom.customer. if it does, it will return callcosts_custom.cost and if it does not exist to return callcosts_price
updated query:
select b.cost
from call_costs_custom b
where a.sequence = b.parent AND b.customer_seq = '124'
union all
select a.retail
from call_costs a
where a.sequence = '4706' and
not exists (select 1 from call_costs_custom b where b.parent = a.sequence);
Yes . . . this sounds like a prioritization query. This typically looks like
select ccc.price
from callcosts_custom ccc
where ccc.?? = ??
union all
select cc.price
from callcosts ccc
where cc.?? = ?? and
not exists (select 1 from callcosts_custom ccc where ccc.?? = cc.??);
It is unclear from your question what the columns are.
This can be done by left joining the tables (under the assumption that every row must exist in callcosts and may or may not exist in callcosts_custom, but not the other way round) and using coalesce. You haven't given too much information about the tables, but I'm assuming there's some ID column to identify the rows:
SELECT c.id, COALESCE(cc.price, c.price) AS price
FROM callcosts c
LEFT JOIN callcosts_custom cc ON c.id = cc.id
This should work for you:
SELECT IFNULL(cc.cost, c.price) cost
FROM callcosts c
LEFT JOIN cc.parent = c.sequence

SELECT if left join IS AND IS NOT ( MYSQL )?

I got one table "people".
Another one with "hobbiesConnection", and they are connected as an OneToMany.
I would like to build a search, where it is possible to search people which have a hobby and don't have a hobby.
Something like :
SELECT * FROM people
LEFT JOIN hobbiesConnection ON hobbiesConnection.people_id =
hobbiesConnection.hobby_id
WHERE hobby_id = 4 AND hobby_id != 5
Now I get the person with the hobby 4, but I want to get an empty result, because both conditions have to fit to the person.
Does anybody have an idea how to accomplish this ?
Firstly, your JOIN is not correct. The two tables should be linked with a foreign key / common column. Try the below query;
SELECT * FROM people
LEFT JOIN hobbiesConnection
ON hobbiesConnection.people_id = people.people_id
And (hobbiesConnection.hobby_id = 4 AND hobbiesConnection.hobby_id != 5)
use (NOT) EXISTS clause:
SELECT * FROM people p
WHERE TRUE
AND EXISTS (
SELECT TRUE FROM hobbiesConnection hc
WHERE hc.people_id = p.people_id -- or p.id? you didnt mention
AND hobby_id = 4
)
AND NOT EXISTS (
SELECT TRUE FROM hobbiesConnection hc
WHERE hc.people_id = p.people_id -- or p.id? you didnt mention
AND hobby_id = 5
)
You can achieve the same using joins
SELECT p.*
FROM people p
JOIN hobbiesConnection h4 ON TRUE
AND h4.people_id = p.people_id -- or p.id? you didnt mention
AND h4.hobby_id = 4
LEFT JOIN hobbiesConnection h5 ON TRUE
AND h5.people_id = p.people_id -- or p.id? you didnt mention
AND h5.hobby_id = 5
WHERE h5.id IS NULL -- or whatever column from that table
have a look at the fiddle: http://sqlfiddle.com/#!9/d9926/4

Mysql query multiple tables retrieve all results even null

I try to make this query to include all products_options_name results even if there are no related rows for the selected language in the information table.
When I run the query now, only products_propoptions are shown that have a related row for selected language in the information table.
Any help is highly appreciated.
Thanks in advance,
Bas
SELECT PPO.products_options_name, I.information_title, PTL.information_id,
COUNT(PPA.products_attributes_id) as amount,
options_id FROM products_propattributes PPA
INNER JOIN products_propoptions PPO ON PPA.options_id = PPO.products_options_id
LEFT JOIN properties_to_information PTL ON PPA.options_id = PTL.option_id
LEFT JOIN information I ON PTL.information_id = I.information_id
WHERE PPO.language_id = 6 AND I.language_id = 6 AND PPA.products_id = 121
GROUP BY PPA.options_id ORDER BY PPA.products_options_sort_order
TABLE: products_propoptions
products_options_id
language_id
products_options_name
products_options_sort_order
TABLE: products_propattributes
products_attributes_id
products_id
options_id
options_values_id
options_values_price
price_prefix
products_options_sort_order
TABLE: information
information_id
information_group_id
information_title
information_description
parent_id
sort_order
visible
noindex
language_id
TABLE: properties_to_information
properties_to_information_id
option_id
information_id
Have you tried changing the INNER JOIN to a LEFT JOIN? The INNER JOIN may be causing the query to omit the resultset rows you're seeking.
This fixed it..
Included LEFT OUTER JOINS with the where conditions in the join...
Thanks..
Bas
SELECT
PPO.products_options_name,
PTL.lexicon_id,
I.information_title,
COUNT(PPA.products_attributes_id) as amount,
options_id
FROM products_propattributes PPA
LEFT OUTER JOIN products_propoptions PPO ON PPA.options_id = PPO.products_options_id AND PPO.language_id = 6
LEFT OUTER JOIN properties_to_information PTL ON PPA.options_id = PTL.option_id
LEFT OUTER JOIN information I ON PTL.lexicon_id = I.information_id AND I.language_id = 6
WHERE PPA.products_id = 121
GROUP BY PPA.options_id ORDER BY PPA.products_options_sort_order

Mysql Query never stops

I have a simple table with 3 columns
PageDescription(varchar(50)) | ParentID (int) | CategoryID (int)
Here is the table data
Fruit||1
Apples|1|2
here is the query i'm running
SELECT PageDescription,(SELECT PageDescription
FROM tblpages a
WHERE a.ParentID = tblpages.CategoryID)
AS 'Page Parent'
FROM tblpages
This query does not stop processing, is there a better way to handle this query?
You can use a JOIN to perform this:
SELECT t1.PageDescription
, t2.PageDescription as PageParent
FROM t t1
LEFT JOIN t t2
on t1.ParentId = t2.CategoryId
See SQL Fiddle with Demo
Using a LEFT JOIN will allow you to include the items that have a null value.
What you need is a join.
SELECT a.PageDescription, b.PageDescription as Parent
FROM tblpages a
LEFT JOIN tblpages b ON a.CategoryID = b.ParentID
Try to name yout two tables:
SELECT b.PageDescription,(SELECT PageDescription
FROM tblpages a
WHERE a.ParentID = b.CategoryID)
AS 'Page Parent'
FROM tblpages b
SELECT p.PageDescription,p1.PageDescription
FROM tblpages p1
LEFT JOIN p2 on p1.parentId=p2.categoryID
WHERE 1;

Return 2 columns using subquerys in main query in mysql

I wish to populate a table on in a mysql database. Firstly I'd like to pull back all of the possibilities and them trim out the unrequired ones (easier than just adding them by hand).
The final table is:
combinations
combID
productID
type
content
exclude
extrafield2
extrafield6
The data comes from
extrafields_values
exvalueID
productID
extrafieldID
content
For each product I need to get return a row for each combination in extra_field_values (extrafieldID = 2 and extrafieldID = 6)
For instance:
productID = 700
extrafield2 = E, D, F
extrafield6 = 34,35,36,37
Returns the exvalueID to extrafields2 and 6 for each combination
So far I've tried:
SELECT EV.productID, extraFieldID, content AS extrafield6,
(SELECT content AS extrafield2
FROM wjf_extrafields_values AS EV2
INNER JOIN wjf_products AS P2
WHERE extraFieldID = 6) AS extrafield2
FROM wjf_extrafields_values AS EV
INNER JOIN wjf_products AS P ON P.productID = EV.productID
WHERE extrafieldID = 6
I believe you just need to link your wjf_products table to your extrafield_values table twice as shown below.
select p.productID, ev1.content as extrafield2, ev2.content as extrafield6
from wjf_products p inner join extrafields_values ev1 on p.productID = ev1.productID
inner join extrafields_values ev2 on p.productID = ev2.productID
where ev1.extraFieldID = 2
and ev2.extraFieldID = 6
Why not just create and use two views instead of a query.
View1 = Combinations
View2 = Subset of View1
Insert data using the View2