I would love some help. I'd like to join a one-to-many database and show all the info from one row in table 'post' with it's related meta (many). I can't seem to figure out how to do it.
Database called post:
ID Title
1 Hello world
2 Yeah buddy
3 This is a test
Database called meta:
ID postID Value
1 1 Testing testing
2 1 This is a value
3 2 Testing 123 testing
4 2 This is a value 23
5 3 Testing testing test
6 3 This is a value yeah
I would like to group the results for my query as follows:
1 Hello world Testing testing This is a value
2 Yeah buddy Testing 123 testing This is a value 23
3 This is a test Testing testing test This is a value yeah
MySQL Query (so far):
SELECT DISTINCT p.title, m.*
FROM post p
LEFT JOIN meta m ON p.ID = m.postID
GROUP BY p.title
Only the output given is:
1 Hello world Testing testing
2 Yeah buddy Testing 123 testing
3 This is a test Testing testing test
Which is really frustrating because I would like to show all the related fields, but I can't seem to figure out what's wrong. It seems the 'Value'-column can't exist twice per row...
Can somebody please help (or point me in the right direction?)
Would it work for you if values are combined?
SELECT p.title, group_concat(m.value separator ",") as values
FROM post p
LEFT JOIN meta m ON p.ID = m.postID
GROUP BY p.title
Related
I don't know if the title clears everything up so I'm gonna try to explain it here.
So I have a table like this
Project ID Project topic
1 topic1
1 topic2
1 topic3
2 topic1
3 topic2
.. ...
What I want to do is: Count the number of projects (Project IDs) which contain both topic1 and topic3 for example.
I hope I made the question clear. The solution might be very obvious but somehow I can't figure it out.
Thank You!
SELECT COUNT(DISTINCT p1.project_id)
FROM project p1 JOIN
project p2 ON (p1.project_id = p2.project_id)
WHERE p1.project_topic = 'topic1'
AND p2.project_topic = 'topic2';
If you wanted topic1 and topic3, then change topic2 to topic3.
Say I've two tables products and brands having below data.
tbl_products
ID Name BrandID Price
1 Keyboard 1 100
2 Keyboard 2 120
3 Keyboard wireless 1 130
4 Keyboard wireless 2 150
tbl_brands
ID Name
1 Microsoft
2 Dell
3 HP
What I want is when I type 'Microsoft Keyboard' or 'Keyboard Microsoft' then it should list me product ID 1 and 3 not 2 or 4 even 2 or 4 has keyboard. I may search for more keywords but it should give me only the items matching itself.
SELECT p.*, b.Name BrandName FROM tbl_products p INNER JOIN tbl_brands b ON b.ID = p.BrandID WHERE p.Name LIKE '%Microsoft%' OR b.Name LIKE '%Microsoft%' OR p.Name LIKE '%Keyboard%' OR b.Name LIKE '%Keyboard%'
Please help me to write proper MySQL query or any schema change with query..
Appreciate the question. Though I don't have exact answer but surely can discuss one approach to solve the problem.
STEP 1 Get BRAND NAME+NAME as single string.
STEP 2 Tokenise the entered STRING. Example Microsoft Keyboard = Mincrosoft,Keyboard Following Link for spliting the entered data. How do I split a string so I can access item x?
STEP 3 DO a like query on the string obtained in step 1.
Good Morning,
I have a database used to register firefighters for examinations, I would like to export my query of testing sites into a spread sheet or database but I have been running into a simple problem.
I have two tables, tblTestSite and tblMaster
tblTestSite looks like this:
comSiteString // Primary key, Short Text that includes date-time-location (One to many relationship to tblMaster.testSite)
Deadline //Date&Time
Site Address //Short Text
Proctor //Short Text
tblMaster looks like this
ID //Primary Key
testSite // Short Text (This is the field being counted in my query)
departmentName //Short Text
lastName //Short Text
firstName //Short Text
testingLevel //Short Text
Here is the SQL for the query:
SELECT tblTestSite.comSiteString, Count(tblMaster.testSite) AS CountOftestSite
FROM tblTestSite INNER JOIN tblMaster ON tblTestSite.comSiteString = tblMaster.testSite
GROUP BY tblTestSite.comSiteString;
And the result:
Testing Site|Number of tests
Test Site 1 | 12
Test Site 2 | 23
My Desired result would be for the query to show all testing sites, including ones that have not yet had any bookings.
Testing Site|Number of tests
Test Site 1 | 12
Test Site 2 | 23
Test Site 3 | 0
I would like for this to work so that I can easily update our online calendar to show the number of seats remaining at a given test site. If I can just get my query to include nulls, then I can easily do the rest of the work.
If you are going to give me code to insert, please be very specific as to where the code goes into my access query.
Thanks!
Use a LEFT JOIN instead of INNER JOIN.
I am very new to SQL. I have done some basic "select from where ..." queries but I struggle with my current project.
Lets say this is my source table:
Project Involved
1 Harald
1 Kerstin
1 Peter
1 Christian
1 Lisa
1 Linda
2 Sören
2 Schmidt
2 Jörg
2 Robert
2 Harald
2 Lisa
My question should be fairly simple. The input is the name "Lisa" and "Harald". I want to know "Which projects are Lisa and Harald involved in"
If this is super easy and cannot understand why I ask such easy thing: provide me with a link where this is explained and ill read trough it myself, just not so sure what exactly to look for so I thought this was a faster way to get started :)
You can solve this in many ways, but here is the primary way I would solve it... but start simply for what projects LISA is associated with... This prevents looking at what could be 1000s of projects / people but if Lisa is only associated with 5 (or 2 in this case), why query against all of them..
Select
p1.project
from
project p1
where
p1.involved = 'Lisa'
So this lists projects 1 & 2 (obviously your short sample of data. Now, that we know this works, I would just JOIN again for Harold and the same project as this.
Select
p1.project
from
project p1
join project p2
on p1.project = p2.project
AND p2.involved = 'Harold'
where
p1.involved = 'Lisa'
Ensure you have an index on (involved, project) to help optimize the query
Additionally, others may propose to do a group by and having clause based on both parties you are interested in.... something like
select
p1.project
from
project p1
where
p1.involved in ( 'Lisa', 'Harold')
group by
p1.project
having
count(*) = 2
This basically says to the engine. Give me each project where either Lisa or Harold exist. But, by applying a group by I only want the project to show once so I don't see duplicates. The HAVING clause tells how many you EXPECT to have per project, and since you are asking for 2 possible names, and want both of them, the HAVING COUNT(*) is 2 so you know BOTH are included.
I've named table as person, so this is the example
SELECT
p1.project
FROM
person p1,
person p2
WHERE
p1.name = 'Harald'
AND p2.name = 'Lisa'
AND p1.project = p2.project
To see the projects which both Lisa and Harald are involed in:
SELECT P1.Project FROM
(SELECT Project FROM MyTable WHERE Involved = 'Lisa') P1
INNER JOIN
(SELECT Project FROM MyTable WHERE Involved = 'Harald') P2
WHERE P1.Project = P2.Project
Anyone could tell me how to connect these 2 tables. I tried do it myself.. but just wasted a time, i know it's easy but I somehow can't understand it. Table is from my previous question
Table Articles:
ID Content
1 bla
2 blah
3 etc.
4 whatever
Table Similar:
ID Similar_ID
3 1
3 2
4 1
4 2
4 3
select a.ID,a.Content,s.Similar_ID from
Articles a inner join Similar s
on a.ID=s.ID
You want to browse the Similar table, and "convert" its IDs (e.g. 3) in Content (e.g. "Blah").
So:
SELECT * FROM Similar;
will list all similarities. Since we have two Ids to convert (something is similar to something else), we need two separate JOINS with the same table Articles, and we'll alias them to "a" and "b":
SELECT a.Content, b.Content
FROM Similar
JOIN Articles AS a ON (Similar.ID = a.ID)
JOIN Articles AS b ON (Similar.Similar_ID = b.ID)
;
The first JOIN "decodes" the ID field of Similar, the second decodes "Similar_ID".
So
3 1
becomes now
Etc. Blah
Or you can write:
SELECT CONCAT(a.Content, ' is similar to ', b.Content)
FROM... (same query as above)
and get
Etc. is similar to Blah
Blah is similar to whatever
...