I have 2 tbl, author and posts.
+---------------+
| AUTHOR |
+---------------+
id | name
+-----------------+
| POST |
+-----------------+
|id |title|content|
i want to have the results of select * from author and select * from posts side by side
+------------------------------+
| RESULT |
+------------------------------+
id | name | id | title | content
how to do that? And how to do that for more than 2 columns without using a FK.
OBS: Im using MYSQL
Use join:
SELECT columns FROM Author INNER JOIN Result ON Author.id = Result.id;
Hopefully the id in both tables is the relation.
As we can see there is no relation between the tables, so one solution could be:
select * from author, posts
And it'll create a matrix
you can select two table comumns like this
SELECT authoer.id,authoer.name,posts.id,posts.name,posts.title,post.content from author,posts
if you have relation between two tables your query like this
select authoer.id,authoer.name,posts.id,posts.name,posts.title,post.content from author,posts
where authoer.id=posts.id
you also use JOIN to
try this:
select a.id, a.name, p.id, p.title, p.content
FROM author a
INNER JOIN post p ON a.id = p.id;
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
TRY THIS:
SELECT A.ID,A.NAME,B.ID,B.TITLE,B.CONTENT
FROM POST A
INNER JOIN AUTHOR B
ON A.ID=B.ID
Related
My database has two tables.
food_table:
+----+----------+---------------+
| ID | NAME | NutrientID |
+----+----------+---------------+
nutrient_table:
+------------+--------------+
| NutrientID | NutrientName |
+------------+--------------+
I want select all rows in food table, but get NutrientName instead of nutrientID.
If I do:
select * from food_table.
I will get NutrientID. Is it possible to get NutrientName in a single query?
SELECT f.*, n.NutrientName FROM food_table f
LEFT JOIN nutrient_table n
ON n.NutrientID = f.NutrientID
You need to make a join inner to tables filtering by field NutrientID , if you look this field as the same in two table and the join works fine.
im not good at "making" query. Let's say this is my db:
artist pics
------------------- ---------------------------------------
| id | name | age | | id_artist | picUrl | picDescription |
------------------- ---------------------------------------
| /\
----------------------------
I have to make a select to get a (single) artist with all its pics. The artist is always one and the pics are usually many.
Is there a way to get this data with just one query? After the query is made, how can I handle the table fields to retrieve their values?
I need to "print something like this"
michael jackson
-pics 1 blablabla
-pics 2 blablabla
-pics 3 blablabla
....
Thank you
Try this:-
SELECT A.id, A.name, B.picUrl, B.picDescription
FROM artist A, pics B
WHERE A.id = B.id_artist;
You should get numerous help if you have Googled around.
Anyways, You would want to use following query:
SELECT * from artist ar INNER JOIN pics p ON ar.id=p.id_artist
You have to use joins :
select * from artist a inner join pics p on a.id=p.id_artist
This query will not give you artists that do not have pics. To get them :
select * from artist a left join pics p on a.id=p.id_artist
Rgds,
Use Joins:
SELECT a.*, b.picUrl, b.picDescription FROM artist a
INNER JOIN pics b on b.id_artist = a.id
You can use a inner join between these two tables. This will show all rows from both tables as long as there is a match.
SELECT column_name(s)
FROM artist
INNER JOIN pics
ON artist.id=pics.id_artist;
Introduce a Where clause for a specific atrist if you only want to get a single value returned. E.g.
WHERE artist.id=001
SELECT
id,
`name`,
picurl,
picdescription
FROM
artist
INNER JOIN pics
ON artist.`id` = pics.`id_artist` ;
I have Three tables,
Posts,
Tags,
Posts_Tags_Link
Posts has:
id, content
Tags has: id, tag
Posts_Tags_Link has: post_id, tag_id
Basically if a tag is linked to a post then an entry is created in Posts_Tags_Link as this is a many-many relationship.
Anyway, I want to do some searches and return all rows from Posts that are linked to a particular keyword.
E.g. If I have the
Posts:
id | content
1 | some stuff
2 | more stuff
3 | stuff again
Tags:
id | tag
1 | first
2 | second
3 | third
4 | fourth
Posts_Tags_Link
post_id | tag_id
1 | 1
1 | 2
2 | 2
3 | 3
3 | 4
and I search for second I want to return
id | content
1 | some stuff
2 | more stuff
I assume I am to use a join for this,
Would I just join my posts table to the link table, on the post_id and join the link table to the link table to the tags table on the tag_id column?
I believe that is right, but If I only want to rows that match the search (like not where) would I use like or would one of the different joins work?
I want that if I search for sec it would have the same result as if I searched for second so believe that I have to do this using like?
You should join the three tables since you want to search from them, example
SELECT a.*
FROM post a
INNER JOIN Posts_Tags_Link b
on a.id = b.post_id
INNER JOIN Tag c
ON b.tag_tag_id = id
WHERE a.content like '%keyword%' OR -- build you conditions here
c.tag like '%keyword%'
Try to use the following query.
SELECT p.id, p.content FROM
Posts_Tags_Link ptl
INNER JOIN Posts p ON p.id = ptl.post_id
INNER JOIN Tags t ON t.id = ptl.tag_id
WHERE t.tag = 'second'
I have following two tables 'USERS' and 'GROUPS':
USERS
-id
-name
-groupid
GROUP
-id
-name
I'd like to return all users along with their group's name and group id. It should be an outer join on group id field correct?
A simple INNER JOIN should be enough:
SELECT `USERS`.*, `GROUP`.name AS group_name
FROM `USERS`, `GROUP`
WHERE `USERS`.groupid = `GROUP`.id
You're going to want to look at the JOIN statement
Doing this from my phone, so pardon any moderately incorrect syntax, but something a long the lines of
Edit: other guy's syntax is better. It's too early here
You can use a LEFT JOIN between users and groups so that users who are not in a group still show up in the result set, but with group name and id NULL:
SELECT
a.*,
b.name AS group_name
FROM
users a
LEFT JOIN
`group` b ON a.group_id = b.id
Side note: Ensure that you're encasing the table name group in backticks because it is a reserved keyword.
The result-set should look something like:
id | name | group_id | group_name
-----------------------------------------------------------------------------
1 | John | 5 | ThisIsGroup5
3 | Tim | 3 | ThisIsGroup3
6 | NotInGroup | NULL | NULL
Changing LEFT to INNER in the above query would INNER JOIN the two tables and exclude the user "NotInGroup" from the result-set.
I don't understand MySQL very well, here are the table structures I am using.
users
id | first_name | last_name | username
| password
categories
id | user_id | name | description
links
id | user_id | category_id | name |
url | description | date_added |
hit_counter
I am trying to return a result set like this, to give information about the category for a user that includes how many links are in it.
id | user_id | name | description | link_count
At the moment I have this query, but it only returns rows for categories that have links. It should return rows for categories that do not have any links (empty categories).
SELECT categories.*, COUNT(links.id)
FROM categories LEFT JOIN links ON
categories.id=links.category_id;
How to do this query? Thanks.
we can't do select table dot "star" with an aggregate.
what you wanna do is something like (pseudocode):
select
categories.field1,
categories.field2,
{etc.}
count(links.id)
from categories
left join links
on categories.id = links.category_id
group by
categories.field1,
categories.field2,
{etc.}
iow: you're missing the group by code-block to get the right aggregate in your query result set.
To mold alien052002's answer to fit your specific question, the following (untested) should work:
select c.id,
c.user_id,
c.name,
c.description,
count(l.link_count)
from categories c
left join links l on l.category_id = c.id
group by c.id, c.user_id, c.name, c.description
try this
SELECT categories.*, COUNT(links.id) FROM categories LEFT JOIN links ON categories.id=links.category_id group by categories.id;