MySQL query ignoring NOT argurment - mysql

I am trying to run that filters certain data from the result, including making sure that none of the records that have 'A_null_choice' as a value for it's moduleName.
$sql = "SELECT am.moduleID, m.moduleName, am.type, s.regNum, s.award
FROM tbl_awardModules am
LEFT OUTER JOIN tbl_module m ON am.moduleID = m.moduleID
INNER JOIN tbl_awardLevels al ON am.awardLevelID = al.awardLevelID
INNER JOIN tbl_award a ON al.awardID = a.awardID
INNER JOIN tbl_student s ON a.awardID = s.award
WHERE am.type <> 'C' AND regNum = '$student_id' AND m.moduleName NOT LIKE 'A_null_Choice'";
The rest of the query runs fine and there are no errors etc, it just keeps showing the records with that value.
Any insights will be greatly appreciated, thanks

Related

How to use where clause within my big join Mysql query to restrict results?

I have successfully written this join query below to get all the data I need from each individual table in the database within one query. Unfortunately, it gives me ALL the data from these individual tables. I only need data that corresponds to e.Design_ID, if e.Design_ID = 1 I have tried a few "where e.Design_ID = 1" within this code block, but have not been successful in restricting the query. I'm stuck. I'm thinking I may need to restructure my entire query. My logic may be off...
Any help is much appreciated.
SELECT
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
";
SELECT
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
WHERE e.Design_ID = 1
";
It should work if you put it at the end. Also check correct spelling.

How to join a few tables without repeat?

I have a SQL query that left joins a few tables in different ways depending on a conditions.
SELECT
dh_partner.company_name,
dh_partner_abonnement.name,
dh_partner_abonnement.description,
dh_partner_abonnement.price,
dh_partner_abonnement.discount_price,
dh_studio.partner_id,
CONCAT('https://some.url/images/studio/logo/', dh_studio.logo) as logo_url,
CONCAT('https://some.url/studio/', dh_studio.alias) as page_url,
CONCAT('https://some.url/order/abonnement/', dh_studio.alias, '/', dh_partner_abonnement_studios.abonnement_id) as checkout_url
FROM dh_partner_abonnement
LEFT JOIN dh_partner on dh_partner.id = dh_partner_abonnement.partner_id
LEFT JOIN dh_studio on dh_studio.partner_id = dh_partner.id
LEFT JOIN dh_partner_abonnement_studios on dh_partner_abonnement_studios.studio_id = dh_studio.id
WHERE
dh_partner.status = 'active'
and dh_partner.id = dh_studio.partner_id
and dh_partner.city_id = '1'
and dh_partner_abonnement_studios.studio_id = dh_studio.id
and dh_studio.show_status = '1'
The challenge in the following code
CONCAT('https://some.url/ru/order/abonnement/', dh_studio.alias, '/', dh_partner_abonnement_studios.abonnement_id) as checkout_url
LEFT JOIN dh_partner_abonnement_studios on dh_partner_abonnement_studios.studio_id = dh_studio.id
and dh_partner_abonnement_studios.studio_id = dh_studio.id
It repeats to the each dh_partner_abonnement this column dh_partner_abonnement_studios.abonnement_id
I have to connect them correctly. I know that I need to use if, however having no idea how.
Just guessing from the table names:
SELECT
p.company_name,
pa.name,
pa.description,
pa.price,
pa.discount_price,
s.partner_id,
CONCAT('https://some.url/images/studio/logo/', s.logo) as logo_url,
CONCAT('https://some.url/studio/', s.alias) as page_url,
CONCAT('https://some.url/order/abonnement/', s.alias, '/', pa.abonnement_id)
as checkout_url
FROM dh_partner_abonnement pa
JOIN dh_partner_abonnement_studios pas ON pas.partner_id = pa.partner_id
AND pas.abonnement_id = pa.abonnement_id
JOIN dh_studio s ON s.id = pas.studio_id
AND s.show_status = 1
JOIN dh_partner p ON p.id = pa.partner_id
AND p.status = 'active'
AND p.city_id = 1
You haven't joined dh_partner_abonnement and dh_partner_abonnement_studios on the complete key, which should include the abonnement_id.
It also seems a studio has a partner_id to indicate that it belongs to a certain partner. However, in your query you are interested in the abbonement studios, so join the studio on the abbonement's studio_ids.

5 left joins cause query to time out, mysql 5.5

The whole point is to get all artist records, and any matching records in 5 other tables.
There are maybe 50,000 artist records.
I plan on creating a view out of this statement and then using it to keep a sugarcrm db up to date.
Any help is hugely appreciated.
here is the SQL statement,
SELECT
/*foreign keys*/
band_profiles.id AS 'BP_ARTIST_ID',
bandsintown_artists.id AS 'BID_ARTIST_ID',
contacts.id AS 'CONTACTS_MGMT_ID',
facebook_stats.id AS 'FB_STATS_ID',
outbound_links.id AS'OUTB_LINKS_ID',
/*high level*/
band_profiles.name AS 'ACCOUNT_NAME',
band_profiles.description AS 'ACCOUNT_DESCRIPTION',
band_profiles.bandsintown_status AS 'BIT_STATUS',
band_profiles.created_at AS 'DR_CREATED_DATETIME',
band_profiles.updated_at AS 'DR_UPDATED_DATETIME',
/*account mgmt fields*/
contacts.description AS 'ACCOUNT_MGMT_CONTACT',
contacts.contact_type AS 'ACCOUNT_MGMT_TYPE',
contacts.custom_contact_type AS 'ACCOUNT_MGMT_TYPE_C',
/*account web & social*/
band_profiles.website_url AS 'WEBSITE_URL',
band_profiles.twitter_url AS 'ACCOUNT_TWITTER_URL',
band_profiles.facebook_url AS 'ACCOUNT_FACEBOOK_URL',
band_profiles.facebook_page_id AS 'ACCOUNT_FACEBOOK_ID',
band_profiles.facebook_like_count AS 'ACCOUNT_FACEBOOK_LIKES',
facebook_stats.like_count AS 'FB_TOTAL_LIKES',
facebook_stats.share_count AS 'FB_TOTAL_SHARES',
facebook_stats.comment_count AS 'FB_TOTAL_COMMENTS',
facebook_stats.click_count AS 'FB_TOTAL_CLICKS',
outbound_links.target_url AS 'OUTBOUND_LINK',
outbound_links.link_type AS 'OUTB_LINK_TYPE',
bandsintown_artists.facebook_tour_dates_url AS 'ACCOUNT_FB_TOUR_DATES'
FROM band_profiles
LEFT JOIN bandsintown_artists
ON band_profiles.id = bandsintown_artists.band_profile_id
LEFT JOIN contacts
ON band_profiles.id = contacts.id
LEFT JOIN facebook_stats
ON band_profiles.id = facebook_stats.band_profile_id
LEFT JOIN outbound_links
on band_profiles.id = outbound_links.band_profile_id
GROUP BY band_profiles.id
LIMIT 10
this is the error code I am getting
Error Code: 2013. Lost connection to MySQL server during query 600.000 sec
If you are the sysadm, you can increase the mysql timeout values. see http://www.rackspace.com/knowledge_center/article/how-to-change-the-mysql-timeout-on-a-server
Depending on the number of deleted entries in your db, you might see significant memory/time savings in this query by pruning deleted entries in your JOIN queries
also limit your final output by band_profiles.deleted = 0
LEFT JOIN bandsintown_artists
ON (band_profiles.id = bandsintown_artists.band_profile_id AND bandsintown_artists.deleted = 0)
LEFT JOIN contacts
ON (band_profiles.id = contacts.id AND contacts.deleted = 0)
LEFT JOIN facebook_stats
ON (band_profiles.id = facebook_stats.band_profile_id AND facebook_stats.deleted = 0)
LEFT JOIN outbound_links
on (band_profiles.id = outbound_links.band_profile_id AND outbound_links.deleted = 0)
WHERE band_profiles.deleted = 0

Simple query issue with multiple tables and mismatching IDs

I'm having trouble with a simple MySQL Query.
Here is the query:
SELECT distinct e.E_CODE, s.S_CODE, p.P_ID, p.P_NAME, p.P_FIRSTNAME, p.P_STATUS, e.E_BOSS, tp.TP_TITLE
from event_participation ep, worker p, type_participation tp, event e, section s
where ep.P_ID = p.P_ID
and s.S_ID = e.S_ID
and ep.TP_ID = tp.TP_ID
and e.E_CODE = ep.E_CODE
The problem is that ep.TP_ID sometimes has a value set to zero while tp.TP_ID has nothing with a zero ID. It's auto-increment and starts at 1 and so on.
The result is obviously that this query does not return records when the ep.TP_ID = 0 and there is no match in tp.TP_ID.
So I'm trying to figure out a way to get those results in there anyway. I was thinking of using a LEFT JOIN statement but couldn't figure out a proper way to insert it into the query.
Any advice on this matter would be greatly appreciated.
First of all, I advice you to use some general type for event_participation records without type; But, unless to take that decision, supposing you want to get all matching records between all tables but also get results with no type, you can use the following query:
SELECT DISTINCT e.E_CODE, s.S_CODE, p.P_ID, p.P_NAME, p.P_FIRSTNAME, p.P_STATUS, e.E_BOSS, tp.TP_TITLE
FROM event_participation ep
JOIN worker p ON (ep.P_ID = p.P_ID)
JOIN event e ON (e.E_CODE = ep.E_CODE)
JOIN section s ON (s.S_ID = e.S_ID)
LEFT JOIN type_participation tp ON (ep.TP_ID = tp.TP_ID)
SELECT DISTINCT e.E_CODE
, s.S_CODE
, p.P_ID
, p.P_NAME
, p.P_FIRSTNAME
, p.P_STATUS
, e.E_BOSS
, tp.TP_TITLE
FROM event_participation ep
JOIN worker p
ON p.P_ID = ep.P_ID
JOIN event e
ON e.E_CODE = ep.E_CODE
JOIN section s
ON s.S_ID = e.S_ID
LEFT
JOIN type_participation tp
ON tp.TP_ID = ep.TP_ID;

Convert this SQL to HQL? (left join with values instead of columns)

I'm trying to convert this SQL to HQL, but I found no way to do that left join.
SELECT
mdcs_causa.id_causa,
usuarios.ds_usuario,
usuarios.setor,
empresas.ds_empresa,
itens_controle.id_item_controle,
itens_controle.ds_item,
itens_controle.ds_indicador,
itens_controle.ds_cliente,
itens_controle.desdobramento,
itens_controle.auxiliar,
itens_controle.bmk_nome,
itens_controle.bmk_vlr,
m.status,
m.medido,
m.medicao,
m.fca,
m.am_cronico,
m.ac_cronico,
m.ap_cronico,
m.id_medicoes as idmedicao,
itens_controle.prioridade
FROM
mdcs
INNER JOIN mdcs_causa
ON mdcs.id_mdc = mdcs_causa.id_mdc
INNER JOIN itens_controle
ON mdcs_causa.Id_Item_Controle = itens_controle.id_item_controle
INNER JOIN usuarios
ON usuarios.id_usuario = itens_controle.id_usuario
INNER JOIN empresas
ON empresas.id_empresa = usuarios.id_cliente_tabela
LEFT JOIN medicoes m
ON (
itens_controle.id_item_controle = m.id_item_controle
and
m.nm_ano = 2013
and
m.nm_periodo = 2
and
mdcs_causa.id_mdc = mdcs.id_mdc
)
WHERE
mdcs.id_mdc = 5077
I thought I could put the nm_ano and nm_periodo conditions in the where clause, with an OR to m.id_item_controle is null but this OR condition didn't seem to work, even in SQL.
Another approach was to left join a sub query. That worked in SQL, but I think HQL doesn't support that.
It can be done with with keyword:
...
left join itens_controle.medicoes m with (m.nm_ano = 2013 and m.nm_periodo = 2 and mdcs_causa.id_mdc = mdcs.id_mdc)
where mdcs.id_mdc = 5077
Instead of explicit values you should supply them as arguments.
Read this in this pdf left join is used
as below:
Query getEBills =session.createQuery( session.createQuery( from " EBill ebill EBill
ebill
left join ebill.accountTransaction where
ebill.balance > 500";
Li li f l bi i li () ist listOfRowValues = getDebitTransactions.list();
for (Object[] singleRowValues : listOfRowValues) {
// pull off the EBill // pull off the EBill
EBill ebill = (EBill)singleRowValues[0];
I dont have any experience in HQL, i have pasted what i have found for you. hope this will help you in any way.