Strange behaviour in continuous form - ms-access

I have a table of employees with names and two surnames (in Spain two surnames are required).
To avoid erroneous entries, I have ONE surname table called "ST_JLE_Apellidos". In this way, what I store is the surname number, not the surname itself.
To feed the form I use the following SQL string (Where Table "Apellido2" is an instance of Table "Apellido1":
SELECT
JLE_Personal.Id_Personal_Personal,
JLE_Personal.Personal_NIF,
Apellido1.Personal_Apellido,
Apellido2.Personal_Apellido,
ST_JLE_Personal_Nombres.Personal_Nombre,
ST_JLE_CargosEnLaEmpresa.CargosEnLaEmpresa_CargoEnLaEmpresa,
ST_JLE_Municipios.Municipios_Municipio,
ST_JLE_Provincias.Provincias_Provincia,
ST_JLE_Paises.Paises_NombreCast,
JLE_Personal.Personal_Alta_FechaInicial,
JLE_Personal.Personal_Alta_FechaFinal
FROM
ST_JLE_Personal_Apellidos AS Apellido2
INNER JOIN (
ST_JLE_Personal_Apellidos AS Apellido1
INNER JOIN (
(
ST_JLE_Paises
INNER JOIN ST_JLE_Provincias ON ST_JLE_Paises.Id_Paises_Pais = ST_JLE_Provincias.Cod_Provincias_Pais
)
INNER JOIN (
ST_JLE_Municipios
INNER JOIN (
ST_JLE_CargosEnLaEmpresa
INNER JOIN (
ST_JLE_Personal_Nombres
INNER JOIN JLE_Personal ON ST_JLE_Personal_Nombres.Id_Personal_Nombre = JLE_Personal.Cod_Personal_Nombre
) ON ST_JLE_CargosEnLaEmpresa.Id_CargosEnLaEmpresa_CargoEnLaEmpresa = JLE_Personal.Cod_Personal_Cargo
) ON ST_JLE_Municipios.Id_Municipios_Municipio = JLE_Personal.Cod_Personal_Municipio
) ON ST_JLE_Provincias.Id_Provincias_Provincia = ST_JLE_Municipios.Cod_Municipios_Provincia
) ON Apellido1.Id_Personal_Apellido = JLE_Personal.Cod_Personal_Apellido1
) ON Apellido2.Id_Personal_Apellido = JLE_Personal.Cod_Personal_Apellido2;
Here is the MS-Access graphic window of the query:
And here is the result:
Everything correct so far.
The problem comes when I try to use a continuous form that contains the two last names:
TextBox 1: Apellido1.Personal_Apellido
TextBox 2: Apellido2.Personal_Apellido
MS-Access accepts both text boxes, but when I run the form, then Text Box 2 automatically changes to Apellido1.Personal_Apellido:
What could be the mistake that I am making?
Thanks in advance

Related

Returning value from query based on results

I have this query I'm trying to build to display specific information for a stored table. I'm needing the query to also display the Enemy Guild Name but I'm having trouble getting the query to take the Enemy Guild ID and link it to the name.
SELECT g.wPos as wPos, g.szGuildName as szGuildName, g.dwGuildExpWeek as dwGuildExpWeek, g.dwEnemyGuildID as dwEnemyGuildID, gm.wPower as wPower, gd.szName as szName
FROM guild as g
LEFT JOIN guild_member AS gm ON gm.dwGuildID = g.dwGuildID AND gm.wPower = '1'
LEFT JOIN gamedata AS gd ON gd.dwID = gm.dwRoleID
WHERE g.wPos = '1'
The output of the query right now results in the following:
Query Results Currently
What I need it to do now is take the dwEnemyGuildID it finds and then use that ID to search for the szGuildName while also displaying the other data it finds.
Use the concept of SELF JOIN, in which we will join same table again if we have a field which is a reference to the same table. Here dwEnemyGuildID is reference to the same table.
A trivial example of the same is finding Manager for an employee from employees table.
Reference: Find the employee id, name along with their manager_id and name
SELECT
g.wPos as wPos,
g.szGuildName as szGuildName,
g.dwGuildExpWeek as dwGuildExpWeek,
g.dwEnemyGuildID as dwEnemyGuildID,
enemy_g.szGuildName as szEnemyGuildName, -- pick name from self joined table
gm.wPower as wPower,
gd.szName as szName
FROM guild as g
LEFT JOIN guild_member AS gm ON gm.dwGuildID = g.dwGuildID AND gm.wPower = '1'
LEFT JOIN guild AS enemy_g ON g.dwEnemyGuildID = enemy_g.dwGuildID -- Use self join
LEFT JOIN gamedata AS gd ON gd.dwID = gm.dwRoleID
WHERE g.wPos = '1';

MySQL create Tables/Views

I don't know if the title is correct but here goes my problem.
So, I want to create View in phpmyadmin using data from several tables (picture below), View that represents maintenance for lamps with fields from several tables (substation, post_type, area, lamp_type, failure and maintenance)
Here are tables with connections:
I've manage somehow and created something like this(picture below), which I managed to create using this block of code:
CREATE OR REPLACE VIEW
v_lamp_I
AS
SELECT
lamps.id,
substation.code AS sub_code,
substation.name AS sub_name,
lamps.lamp_code,
post_type.descript AS post_ty,
lamp_type.descript AS lamp_ty,
area.descript AS area_name,
rasvjeta.adress,
DATE_FORMAT(date_maintenance, "%d.%m.%Y.") AS date_main,
lamps.geo_long,
lamps.geo_lat
FROM lamps
INNER JOIN substiation ON substiation.id = lamps.substiation_id
INNER JOIN post_type ON post_type.id = lamps.post_type_id
INNER JOIN lamp_type ON lamp_type.id = lamps.lamp_type_id
INNER JOIN area ON area.id = rasvjeta.area_id
INNER JOIN maintenance ON maintenance.lamps_id = lamps.id
I've managed to create view but the problem is with that view I can see only rows/lamps(sifra_lampe) which were maintained, only 4. In table lamps
I've 24 entries in and only 4 entries for maintenance.But, I want to see all 24 entries and if there was no maintenance for that particular lamp, field can be empty with date format (00-00-00 or it can be NULL) and for entries/lamps that were maintained I want to be visible date field.
Here is table lamps with entries.
And here is view with maintenance date. As you can see there are only 6 entries
I want to see the rest of the entries, for lamps that were not maintained entries can be null or date format like this (00-00-00) and for lamps that were maintained date format can stay the same, in short I want to see all entries not only those which were maintained. Thank you and sorry for long question. I didn't know how to construct meaningful and short question so wrote everything.
create or replace view
v_lamp_I as
SELECT lamps.id,
substation.code AS sub_code,
substation.name AS sub_name,
lamps.lamp_code,
post_type.descript AS post_ty,
lamp_type.descript AS lamp_ty,
area.descript AS area_name,
rasvjeta.adress,
lamps.geo_long,
lamps.geo_lat
FROM lamps
INNER JOIN substiation ON substiation.id = lamps.substiation_id
INNER JOIN post_type ON post_type.id = lamps.post_type_id
INNER JOIN lamp_type ON lamp_type.id = lamps.lamp_type_id
INNER JOIN area ON area.id = rasvjeta.area_id;
CREATE OR REPLACE VIEW
v_lamp_new
AS
select v_lamp_I.*,DATE_FORMAT(date_maintenance, "%d.%m.%Y.") AS date_main
from v_lamp_I
LEFT JOIN maintenance ON v_lamp_I.id = maintenance.lamps_id;
CREATE OR REPLACE VIEW
v_lamp_I
AS
select tbl_lamp.*,DATE_FORMAT(date_maintenance, "%d.%m.%Y.") AS date_main
from (SELECT lamps.id,
substation.code AS sub_code,
substation.name AS sub_name,
lamps.lamp_code,
post_type.descript AS post_ty,
lamp_type.descript AS lamp_ty,
area.descript AS area_name,
rasvjeta.adress,
lamps.geo_long,
lamps.geo_lat
FROM lamps
INNER JOIN substiation ON substiation.id = lamps.substiation_id
INNER JOIN post_type ON post_type.id = lamps.post_type_id
INNER JOIN lamp_type ON lamp_type.id = lamps.lamp_type_id
INNER JOIN area ON area.id = rasvjeta.area_id) as tbl_lamp
LEFT JOIN maintenance ON tbl_lamp.id = maintenance.lamps_id

distinct in query repeating values

I wonder if anyone can spot the problem with this 'view' query to show only 'company' and 'materials'. The problem is that it shows each entry a separate time for each material and company. I'm using this view to populate a dropdown box in a form but I would only like it to show the distinct values for each column (company/materials) - as of now if, for example, end result is the company 'Anderson' twice but with different materials for each...it shows 'Anderson' twice. I've tried using DISTINCT after the select statement for each of the two, but I don't achieve what I want.
select `b`.`company` AS `company`, `bp`.`material` AS `material`
from (((`caseys_wrdp4`.`windows_brands_products` `bp`
left join `caseys_wrdp4`.`windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`)))
join `caseys_wrdp4`.`Windows_last_submissions` `ls`)
join `caseys_wrdp4`.`windows_materials` `wm`)
where ((`bp`.`width` = round(`ls`.`width`,0))
and (`bp`.`height` = round(`ls`.`height`,0))
and (`bp`.`material` = `wm`.`name`)
and (`bp`.`type` = `ls`.`type`)
and if ((`ls`.`minimumbid` <> '0.00'),
(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),
(`bp`.`cost` <= `ls`.`maximumbid`)))
Possible answer:
Add GROUP_CONCAT and GROUP BY to your query:
select `b`.`company` AS `company`, GROUP_CONCAT(`bp`.`material`) AS `materials`
from (((`caseys_wrdp4`.`windows_brands_products` `bp`
left join `caseys_wrdp4`.`windows_brands` `b` on((`bp`.`brand_id` = `b`.`id`)))
join `caseys_wrdp4`.`Windows_last_submissions` `ls`)
join `caseys_wrdp4`.`windows_materials` `wm`)
where ((`bp`.`width` = round(`ls`.`width`,0))
and (`bp`.`height` = round(`ls`.`height`,0))
and (`bp`.`material` = `wm`.`name`)
and (`bp`.`type` = `ls`.`type`)
and if ((`ls`.`minimumbid` <> '0.00'),
(`bp`.`cost` between `ls`.`minimumbid` and `ls`.`maximumbid`),
(`bp`.`cost` <= `ls`.`maximumbid`)))
GROUP BY(`b`.`company`);
This will give you back a single row for each company w/ company and materials. materials will be a comma-separated list (#Barmar). You could then parse that field for the 2nd drop down.
Example Rows:
'Anderson' 'Wood,Vinyl'
'WM' 'Metal','Plastic'
Assuming you're building a webpage, depending on how you're building the DOM, you can either use javascript str.split(), https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split, or on the server side, any language should work.

mysql create a hardcoded value within select statement

I want to know if I can hardcode a value within my select statement. I have the following mysql query that I use to generate a list.
I use concat to build this string as part of creating the list.
However, now I need to generate an 'Unknown' record as part of the list.
For example:
20(ABC Object #20)
24(DEF Object #24)
I want to add a value of 'UKNOWN' to the top of the list:
--(UNKNOWN -- )
20(ABC Object #20)
24(DEF Object #24)
Here is what I have so far:
SELECT CAST(p.Serial AS UNSIGNED INTEGER) as Serial,
p.Object_ID,
p.Part_ID,
st.Description AS ObjectDesc,
s.Object_Num,
concat(Serial,' (',st.Desc,' #',s.Object_Num,')') as DropList
FROM Parts p LEFT JOIN Objects s ON p.Object_ID = s.Object_ID
LEFT JOIN ObjectTypes st ON s.ObjectType_ID = st.ObjectType_ID
Can I hardcode that string in my select statement? If so, how do I do that?
If I understand correctly, you want a new row. You can use UNION
SELECT "" as Serial,
"" as Object_ID,
"" as ObjectDEsc,
"" as Object_Num,
"--(UNKNOWN -- )" as DropList
UNION
SELECT CAST(p.Serial AS UNSIGNED INTEGER) as Serial,
p.Object_ID,
p.Part_ID,
st.Description AS ObjectDesc,
s.Object_Num,
concat(Serial,' (',st.Desc,' #',s.Object_Num,')') as DropList
FROM Parts p LEFT JOIN Objects s ON p.Object_ID = s.Object_ID
LEFT JOIN ObjectTypes st ON s.ObjectType_ID = st.ObjectType_ID
In a union query each part must select the same columns. Since your original query included Serial, Object_ID, Part_ID and ObjectDesc, the first part must include it as well.

Taking one column from MySQL joined tables

I have a query in MySQL and I am making a crystal report by using this.
Now inside the query i have a column called scan_mode and it is coming from gfi_transaction table. This scan_mode I am using in report to suppress some sections. But some times this value is coming null for some transaction ids.
So now I want to take this scan_mode as separate query so that it will work.
Can any one please help how I can modify the below query to take only scan_mode column.
SELECT
cc.cost_center_code AS cccde,
cc.name AS ccnme,gf.scan_mode,
cc.cost_center_id AS ccid,
site.name AS siteme,
crncy.currency_locale AS currency_locale,
cntry.language AS LANGUAGE,
cntry.country_name AS cntrynm,
crncy.decimal_digits AS rnd,
gf.transaction_no AS Serial_No,
brnd.name AS brand_name,
rsn.description AS reason,
gf.comment AS COMMENT,
ts.status_description AS STATUS,
DATE_FORMAT(gf.created_date,'%d/%m/%Y') AS created_date,
gf.created_by AS created_by,
IFNULL(gf.approval_no,'Not authorized') AS Trans_no,
gf.approved_date AS approval_dt,
gf.approved_by AS approved_by,gf.status AS status1,
IFNULL(loc.cost_center_code,cc.cost_center_code) AS cur_location,
gf.document_ref_no,gf.document_ref_type,
,DATE_FORMAT(document_ref_date1,'%d/%m/%Y')) AS invoice_no
FROM
gfi_transaction gf
INNER JOIN gfi_instruction gfn ON (gf.transaction_id=gfn.transaction_id)
INNER JOIN gfi_document_instruction doc ON (gf.ref_transaction_no = doc.document_instruction_id)
INNER JOIN reason rsn ON (gf.reason_id = rsn.reason_id)
INNER JOIN gfi_status ts ON (gf.status = ts.gfi_status_id)
INNER JOIN transaction_type tt ON (gf.transaction_type_id = tt.transaction_type_id)
INNER JOIN brand brnd ON(gf.brand_id=brnd.brand_id)
-- cc details
INNER JOIN cost_center cc ON (brnd.parent_brand = cc.brand_id OR gf.brand_id = cc.brand_id)
INNER JOIN site site ON(cc.site_id = site.site_id)
INNER JOIN country cntry ON (site.country_id = cntry.country_id)
INNER JOIN currency crncy ON (cntry.currency_id=crncy.currency_id)
LEFT OUTER JOIN alshaya_location_details loc ON
(gf.brand_id = loc.brand_id AND loc.cost_center_id = gf.cost_centre_id)
LEFT OUTER JOIN alshaya_location_details locto ON
(locto.cost_center_id = gf.from_cost_center_id)
WHERE
gf.transaction_id='{?TransID}'
AND rsn.transaction_type_id IN (10,11,14)
wow, that's a big query. I ran across a similar problem in a query i was building and found the if syntax to be a solution to my problem. This was also answered in this question: MYSQL SELECT WITHIN IF Statement
$psdb->query = "SELECT count, s.classid,
if (k.sic != k.siccode, k.siccode, s.siccode) as siccode,
if (k.sic != k.siccode, k.sicdesc, s.sicdesc) as sicdesc,
if (k.sic != k.siccode, k.sicslug, s.sicslug) as sicslug
FROM ...
It looks like scan_mode column comes from "gfi_transaction" table which seems to be primary table in your query. If you get null for this column then it means your table itself have NULL value for this column. Taking that separately in a query wont solve your problem. Try replacing null with a default value and handle it in code. You can add default value instead of NULL by using ifnull(scan_mode, 'default')