I have a table structured like this (not the actual data or fields, but the same structure applies):
ID | City Field2 Field 3
-------------------------------
1 New York Ohio data
2 Cincinnati data data
3 Los Angeles data Ohio
4 Cleveland data data
Then there's a second table something like this (again, not the actual data)
City State
-------------------------------
Los Angeles California
New York New York
Cincinnati Ohio
Cleveland Ohio
Houston Texas
etc.
I have a php web page that allows users to search the database; when they do, it will automatically print (in my example) the corresponding state from the second table after the city.
However, if the user searches for "Ohio", it should return all four records, even though "Ohio" doesn't exist in the first table. It looks like I'd have to run multiple queries - find the search term in the first table, find it again in the second table, and then search for column 1 of the second table in the first table (and then join the results). Is there an easier way to do that? (I'm fairly new to MySQL.)
Have you tried using a UNION?
SELECT City FROM table1 WHERE Field2 = 'Ohio' OR Field3 = 'Ohio'
UNION ALL
SELECT City FROM table2 WHERE State = 'Ohio'
It would be helpful to see what your query looks like.
Related
I've been trying to find an Access version of the subject and testing many concepts. All I want to do is what I thought was simple. Let's say I have a single column in a table. Values are like
California
Florida
California
California
New York
California
New York
let's leave it that simple. All I'm trying to do which seems easy in SQL Server or Oracle is show the distinct values and how many times they are referenced in the table. So output would be:
California 4
Florida 1
New York 2
On Access 2019 if it matters.
Standard SQL; would be the same in SQL-Server, Oracle, MsAccess (and almost all other SQL dialects); just use:
select State, count(*)
from YourTbl
group by State
Here's my situation : I have a table that has large amounts of records, I need to pull out a number of these records for each name in the database, note that TOP will not work for my use case. My end user wants the report formatted in such a way that each user shows up only once, and up to 3 different dates are shown for the user.
Table format
AutoID
Enum
TNum
Date
Comments
1
25
18
2/2/22
2
25
18
1/2/21
Blah
3
18
18
1/2/21
4
18
18
1/2/20
5
25
17
1/2/22
6
25
17
1/2/20
Now the Enum and TNum fields are fk with other tables, I have created a join that pulls the correct information from the other tables. In the end my query provides this output
RecordID
Training
CompletedDate
FirstName
LastName
Location
2821
MaP
1/1/21
David
Simpson
123 Sesame St.
2822
1/2/22
Fuller
MaP
Dough
GHI
David
123 Sesame St.
2825
1/1/20
Simpson
The two "Blank fields" represent information that is pulled and may or may not be needed in some future report.
So to my question : How do I manage to get a report, with this query's pull to look like this:
Place
LastName
FirstName
Training
FirstCuttoff
Secondcutoff
ThirdCutoff
Comments
123 Sesame St.
David
Simpson
MaP
1/1/20
1/1/21
123 Sesame St.
John
Dough
MaP
1/1/22
I was originally planning on joining my query to itself using where clauses. But when I tried that it just added two extra columns of the same date. In addition it is possible that each record is not identical; locations may be different but since the report needs the most recent location and the name of the trainee. In addition, to add more complexity, there are a number of people in the company with effectively the same name as far as the database is concerned, so rejoining on the name is out. I did pull the Enum in my query, I can join on that if needed.
Is there an easier way to do this, or do I need to sort out a multiple self-joining query?
I have a project I am working on where I am going to have to do this. Some of the suggestions I received were to use a Pivot query. It wouldn't work in my case but it might for yours. Here is a good example
Pivot Columns
This question already has an answer here:
How to output table results by using cfoutput group by date?
(1 answer)
Closed 7 years ago.
I'm working on outputting values from data base in the table. My table has 5 columns: Date, FirstName, LastName, City, State. Here is example of my data base table:
DateMeeting FirstName LastName City State
2015-12-11 Mike Johns Dallas TX
2015-12-11 John Cook Dallas TX
2015-12-11 Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
2015-12-11 Michael Best New York NY
2015-12-11 David Holmes New York NY
So I want to have output table that will display just one date for multiple records. I tried to use DISTINCT on the date and that works fine but if I include my WHERE clause for City and State my query breaks. Also I tried to use GROUP BY but same problem, I can get Date values only once as long as I do not include other columns. In this case I need all columns but my Date value only once. Here is my query that I use:
Select Distinct(DateMeeting),FirstName, LastName, City, State
From Customers
Where City = 'Dallas'
and State = 'TX'
This query does not work with all columns that I have in my select, only if I run DISTINCT(DateMeeting). I would like to output my values in the table to look like this:
Date First Name Last Name City State
Mike Johns Dallas TX
John Cook Dallas TX
Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
Michael Best New York NY
David Holmes New York NY
If anyone knows how this can be done please let me know. Thank you.
Each layer in the technology stack has its strengths and weaknesses.
As for mysql, do not turn it into a report engine as described with blank date columns except for one per date somewhere in the middle of a date chunk as shown. Subsequent dates as ordered will get muddled and confused.
True, one could use slightly interesting mysql variables and dump it just on the first row of a chunk. But for what.
Play to mysql's strengths, return all the data. And have the front-end (coldfusion or whatever), deal with the reporting features you desire for the output.
I have six tables in my database with 45 columns each,with date ans market column repeating in all the tables. 1 million records in it. my date and market values are not unique and is repeating so how can i join these tables together ?
any help ?
For eg:
table 1:
date market col1 col2
1 may India san det
1 may USA lif det
2 may India lif san
table 2 :
date market col3 col4
1 may india san go
1 may USA dif dic
2 may ind det san
RESULT AS
date market col1 col2 col3 col4
1 may India san det san go
1 may USA lif det dif dic
2 may India lif san det san
I tried joining two tables using the query like :
select * from cre,dur where cre.period = dur.period and cre.market = dur.market;
But once i try creating a table its not working .
I joined six tables by hard coding all the column names but is not the right way i think ?
Any HELP ?
You can use in below way-
select cre.*,dur.* from cre,dur where cre.period = dur.period and cre.market = dur.market;
Note: Even your requirement is not much clear to me but as per my understanding you skip cre.period=dur.period then also you can get same results as per your data.
Even better to keep only required column, for this you can follow below steps-
Step1: Export below query results.
select * from table1 limit1;
Step2: Copy all columns from header line and remove unwanted columns.
Further your table structure does not seems good, you should keep one table's reference in another table through parent and child id.
I have a table for users. Each user has certain skills they teach. So for example:
Bob can teach karate
Louise can teach piano and knitting
Roger can teach judo, sailing and fencing
This is how I've done it in the database:
Table users
pk: uid, name
1 Bob,
2 Louise,
3 Roger
Table skills
pk: sk_id, skill
1 karate,
2 piano,
3 knitting,
4 judo,
5 sailing,
6 fencing
Table user_skill (relationship between user and skills)
pk:usk_id, fk:uid, sk_id
1 1 1,
2 2 2,
3 2 3,
4 3 4,
5 3 5,
6 3 6,
I want to then display "Roger has these skills: judo, basketweaving"
select name, skill
from users, skills, user_skill
where users.uid = user_skill.uid
and users.uid = 3
Is this the right way to go about it - both in terms of designing the tables and querying (mysql)?
Then say I want to update their profile with the areas they teach in:
Bob can teach karate in London
Louise can teach piano in Bolton and knitting in Manchester
Roger can teach judo in London and Manchester, sailing in Liverpool and fencing in Bradford
So I add the following tables:
Table cities
pk: city_id, city
1 London,
2 Manchester,
3 Liverpool,
4 Bolton,
5 Bradford,
But I'm confused as to how to do the relationships. I keep writing it out and realizing it doesnt work and starting again so I've obviously gone wrong somewhere.
I would say your general DB structure is fine as far as the relations go. To incorporate the cities aspect you could use your proposed cities table, but also add a column to your user_skill table to include a reference to the city table.
Also make sure you use proper join statements in the select queries as this is best practice and helps the queries run as efficiently as possible.
Can users teach skills in more than one location, e.g. "bob teaches judo in london and bolton"? Or is it strictly one skill-one city?
Depending on how you want your tables, you'd either just add a 'city' field to the user_skills table, and have multiple "bob/judo/cityX" "bob/judo/cityY" type records. Or you'll add yet another table "user_city_skills" where it'd be "user_skill_ID, cityID".
Your Structure looks fine except your usr_skill table. To incorporate the last part add a fk city_id in user_skill table. If the player can teach the same skill in multiple cities, you will need an additional table to avoid multi-valued columns.
Yes, carry on with it. You should also add one more column in table user_skill which will hold city_id.