Join two tables in a particular user session Mysql - mysql

I want to make a query using sql to merge two tables in a database where a particular value matches the current session id.
I'm using dreamweaver and i've added a recordset with this code but it doesn't display anything.
SELECT reg.username, reg.phoneno, gh.pay1
FROM reg INNER JOIN gh ON reg.username = gh.pay1
WHERE gh.username = colname
ORDER BY reg.id DESC
Here is the screenshot http://prntscr.com/ed0mpx
When I take out the first two lines
SELECT reg.username, reg.phoneno, gh.pay1
FROM reg INNER JOIN gh ON reg.username = gh.pay1
It only display record for the first row. Other record doesn't show

Related

Deriving two columns from another table when one of them is being ordered in SQL

I am running this query in MySQL
SELECT id_files, FileAlias, FileEXT, FileSize, FileExtension, UploadDate, category, COUNT(id_downloadInfo) AS "counter", DownloadDate as "lastlydownloaded"
FROM files
LEFT JOIN categories ON files.category_id = categories.id_categories
JOIN users ON files.users_id_users = users.id_users
JOIN downloadinfo ON files.id_files = downloadinfo.files_id_files
WHERE organisations_id_organisations = 1 AND UserOnly = 0 AND files_id_files = 21
ORDER BY DownloadDate DESC LIMIT 1;
It does the job except the lastly download column is not being sorted.
I tried to do this with subquery but you cannot sort within it.
I am trying to connect query with two other tables when one of them have ordered result.
Is it possible to have it within one table?
Return output is
Which is correct except lastly downloaded date which should be:
Here's how the files table looks like
Here's download info query with all the data (users_id_users indicates on specific user).

How to use one table multiple times for inner join in SQL Server?

I am trying to make a table that includes join between 3 tables in the MSSS 2008. There is a fact table, a date table, and a course table. I should join them to make a base table. In date table there is a one parameter that name is Academic Year lookup, and the values in this parameter is like 2000/1, 2001/2. This parameter in the base table should separate to three parameter such as CensusYear, StartYear, and ApplicationYear. Therefore, I need the data table multiple times. I executed a inner join query, and already I have four inner join statement, but I am getting some extra years, and I'm losing some years. I believe, my query should be wrong somewhere.
The attached file is include the design view that created in the MS Access, it'll help to see the tables, and understand what I need to create.
[Design View in Ms Access][1]
SELECT
A.[EventCount],
B.[AcademicYearLookup] AS [CensusYear],
C.[AcademicYearLookup] AS [StartYear],
D.[AcademicYearLookup] AS [ApplicationYear],
B.[CurrentWeekComparisonFlag],
B.[AcademicWeekOfYear],
case
when A.[ApplicationCensusSK] = 1 then 'Same Year'
when A.[ApplicationCensusSK] = 2 then 'Next Year'
when A.[ApplicationCensusSK] = 5 then 'Last Year'
ELSE 'Other'
END as [CensusYearDescription],
B.[CurrentAcademicYear],
A.[StudentCodeBK],
A.[ApplicationSequenceNoBK],
A.[CourseSK],
A.[CourseGroupSK],
A.[CourseMoaSK],
A.[CboSK],
A.[CourseTaughtAbroadSK],
A.[ApplicationStatusSK],
A.[ApplicationFeeStatusSK],
A.[DecisionResponseSK],
A.[NationalityCountrySK],
A.[DomicileCountrySK],
A.[TargetRegionSK],
A.[InternationalSponsorSK] INTO dbo.[BaseTable3yrs]
FROM Student.FactApplicationSnapshot A
INNER JOIN Conformed.DimDate AS B ON A.[CensusDateSK] = B.[DateSK]
INNER JOIN Conformed.DimDate AS C ON A.[AcademicYearStartDateSK] = C.[DateSK]
INNER JOIN Conformed.DimDate AS D ON A.[ApplicationDateSK] = D.[DateSK]
INNER JOIN Student.DimCourse ON A.CourseSK = Student.DimCourse.CourseSK
WHERE (((B.CurrentAcademicYear) In (0,-1))
AND ((A.ApplicationCensusSK) In (1,2,5))
AND ((Student.DimCourse.DepartmentShortName)= 'TEACH ED'));
/* the query to check that the result it's correct or not, and I check it by academic week of year, and I found that I am lossing some data, and I have some extra data, means maybe join is wrong*/
select * from [BaseTable3yrs]
where [StudentCodeBK]= '26002423'
AND [ApplicationSequenceNoBK] = '0101'
order by [AcademicWeekOfYear]
When doing recursive joins like this, it's easy to get duplicate records. You could try gathering the Conformed data separately into a table variable and then joining to it. This would also make your query more readable.
You might also try a SELECT DISTINCT on your main query.

Updated rows not expected values with JOIN?

I've created several tables in a test database based on columns/rows in my main database.
test.websites.url = main.websites.url
test.category_main = main.websites.category1
test.category_01 = main.websites.category2
test.category_02 = main.websites.category3
etc...
The test database columns already contain all the rows from the main
database, but I need to add the rows from the respective tables to the
category_to_website table and create foreign keys because there is
currently no relation between them in the test database. That is why I have joined the
main database in the query.
When trying to use the main table as a reference for updating the existing rows in the test database, some values are updated but they are not always correct. I'm executing the query from the test database.
My query:
UPDATE category_to_website
LEFT JOIN main.websites
ON websites.url = main.websites.url
LEFT JOIN category_01
ON category_01.name = main.websites.category2
SET category_to_website.category_01_id = category_01.id
WHERE category_to_website.category_01_id = main.websites.category2
My database schema:
I suspect that the issue is with the type of JOINs I am doing, but I've tried LEFT JOIN, JOIN, and INNER JOIN and get the same results. I think that maybe I need a SELECT sub query or my WHERE clause is off?
EDIT
Based on the comments I was able to get this all sorted out. Here are the steps I took.
1. Merged the category_* tables into a category table.
2. Joined the test.websites table into the query.
UPDATE test.category_to_website
LEFT JOIN test.websites
ON test.websites.id = category_to_website.url_id
RIGHT JOIN main.websites
ON test.websites.url = main.websites.url
INNER JOIN test.category
ON test.category.name = main.websites.category1
SET category_to_website.category01_id = category.id
WHERE category_to_website.url_id = test.websites.id
UPDATE category_to_website
JOIN websites
ON websites.id = category_to_website.url_id
JOIN main.websites
ON websites.url = main.websites.url
JOIN category
ON category.name = main.websites.category1
SET category_to_website.category01_id = category.id
SETs are done to rows that participate in the JOINs. But if you LEFT JOIN to category_to_website then all its rows participate so then you must restrict them in a WHERE the way you already did in the ON.
Thank goodness you have started to relationalize that horrible schema. Keep going: replace all multiple categery_ columns in each table by just one category column for id or name. And if you named them category_id and category_name their nature would be clear. (Maybe post your next version as a new question.)

Select a relative result set as an array in mysql

So here's my problem. I have 3 tables
case_assign.cfid => case_fields.cfid and case_assign.uid => users.uid.
The single query I am trying to write basically fetches all the cases between two dates while also selecting every person assigned to the case via the case_assign table and the case_assign table also fetches the names via the uid fields in both it and the users table.
Now here's the challenge. Since many people can be assigned to any one case, it will be fetching multiple rows from case_assign for a single row fetched from case_fields but the query isn't case_assign based, its case_fields based which means that I will get an error saying something like "mysql error: too many fields returned" or something like that.
So I am thinking, is there a way I can return the total number of rows selected for any one query gotten from the case_assign table into an array that will be sent to the client then I can probably json.parse() it on the client side.
Thanks
SELECT cf.cfid, GROUP_CONCAT(u.fname) assignees
FROM case_fields cf
LEFT JOIN case_assign ca ON cf.cfid = ca.cfid
LEFT JOIN users u ON ca.uid = u.uid
GROUP BY cf.cfid
This will create a comma-separated list of user names in assignees. You can split this into an array in the client application.
Or you can use this query:
SELECT cf.cfid, u.fname
FROM case_fields cf
LEFT JOIN case_assign ca ON cf.cfid = ca.cfid
LEFT JOIN users u ON ca.uid = u.uid
ORDER BY cf.cfid
Then as you loop through the results, check if the current cfid is the same as the previous one; if it is, append fname to the assignee list, otherwise start a new entry in the result array.

Statement to get complete table ? In SQL Server?

In SQL Server, I am trying to get the rows from the two tables where first table is master table and second is child table. like I have a table which contain file extensions with id, and description of extension. second table is having employees who having extension ids from Master table. now i want to get the total master table and if i place where condition for particular employee his extensionid and description.
When I write left outer join it is getting only that particular employee extension details,
my sql statement like this:
select *
from Tbl_File_Extn_M FEM
left outer join Tbl_File_Extn_EmpLink_M FEEM on FEM.ExtId = FEEM.ExtId
where FEEM.EmpID = '004135'
Result is giving only one row, but I need the complete master table and particular employee details can you help me.
how about
select *
from Tbl_File_Extn_M FEM
left outer join Tbl_File_Extn_EmpLink_M FEEM on FEM.ExtId = FEEM.ExtId AND FEEM.EmpID = '004135'