sql left join with subselection in comma-delimited field value [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to take the column Ngr from table groups WHERE the marks of the students are bad(=2) in more than two subjects and the number of these students are the half from the group:
SELECT Ngr
FROM subject
LEFT JOIN groups ON (subject.Ngr = groups.Ngr)
WHERE....
and here I can't continue
thanks for your future advice!
groups:
| Ngr | Name_of_speciality |
----------------------------
| 1 | Physics |
..........................
subject:
| Ngr | Name | marks |
-------------------------------------------
| 1 | Physics|2,2,3,3,5,5,5,4,3,2,2,5,2,4|
...........................................
Good luck!

Continuing from the comment;
you would have to use LIKE; but wouldn't it be better to redesign your tables to have each mark be a seperate row?
#Luceos, how can I do that?
it depends on your table format; you duplicate Physics in your table, why's that? You have a group called Physics and a subject called Physics, should that be an exam name like Physics 1? Then if you dont want to connect marks to students you can make an auto increment column for primary id:
| id | Ngr | Name | mark |
-------------------------------------------
| 1 | 1 | Physics|2 |
-------------------------------------------
| 2 | 1 | Physics|5 |
...........................................
Etcetera.. This way you can select by mark = 2 or mark <= 2 and get results back in the join.

Related

MySQL 2 tables combine with foreign key - See entries with specific function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 2 tables - the first table has an ID with name and function. The second table has a date and the ID from the first table.
Table 1:
ID | name | func1 | func2
1 | stef | 1 | 1
2 | rob | 0 | 1
3 | bob | 0 | 0
4 | isa | 1 | 0
Table 2:
date | ID
2020-05-01 | 2
2020-05-01 | 4
2020-05-01 | 3
Now I need these IDs from table 2, which have a 1 at "func1". So in this case the ID 1 and 4.
How do I get these?
I tried a lot with MySQL, but didn't find a solution. Only errors.
Thanks and best regards
The query needs a join, as shown below:
select b.ID
from table1 a
join table2 b on b.ID = a.ID
where a.func1 = 1
However this query will find only 4 and not 1, since the latter does not exist in Table 1.

How to create a query to check which sub-String (from DB) is contained in a given String? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to write a query, which will compare the country phone code from one table with the full phone number from another table and will output the appropriate country name.
I was playing playing a bit with the conditional query, but I don't know how to bite it.
My DB contains two tables:
tblCall
CallID | Caller |
____________________________
1 | +4411111111 |
2 | +4911111111 |
tblCode
CodeID | Code | Country |
_____________________________________
1 | +44 | UK |
2 | +49 | Germany |
So I need a query which will check if the code is included in the phone number and will print out the appropriate country name, like this:
CallID | Caller | Country
______________________________________
1 | +4411111111 | UK
2 | +4911111111 | Germany
You would use a join. If the code is always three characters, you can use a comparison such as:
select ca.*, co.country
from tblCall ca left join
tblCountry co
on left(ca.caller, 3) = co.code;
If it is variable, then like:
select ca.*, co.country
from tblCall ca left join
tblCountry co
on ca.caller like concat(co.code, '%')

How to combine two tables into one with a new field with table source? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
table one
+-----+--------+--------+
|date |index |idOne |
|-----+--------|--------+
| | | |
table two
+-----+--------+--------+
|date | index | idTwo |
|-----+--------|--------+
| | | |
query result where index = "5"
+---------+--------+--------+----------+
|date | index | id | idType |
|---------+--------|--------+----------+
|10/10/91 | 5 | 1 | One |
|10/11/91 | 5 | 2 | One |
|10/12/91 | 5 | 1 | Two |
These are related questions but not the same:
This one has no different fields
I want to combine/union two tables and have it create a field that identifies which table it came from
This one has no table distinction field
Combine two tables that have no common fields
You can use the union all operator to join the two queries and add the idType column by selecting a literal:
(SELECT `date`, `index`, idOne AS id, 'One' AS idType
FROM one
WHERE id = 5)
UNION ALL
(SELECT `date`, `index`, idTwo AS id, 'Two' AS idType
FROM two
WHERE id = 5)

Write a Query to Arrange Product and Category of DB table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have two tables one for product and another is category
I have a table with category and subcategory field. I want the summation of different subcategory under a particular category. Like Table
cat_id| category |
------+--------------+--
1 | Cloth |
2 | Food |
3 | Fuel |
4 | School |
and another one is product table
id| name | cat_id |
------+-----------+---------+--
1 | skirt | 1
2 | shirt | 1
3 | St Xavier| 4
4 | Petrol | 3
Now i want result to be
id | name | cat_name |
------+-----------+---------+--
1 | skirt | Cloth
2 | shirt | Cloth
3 | St Xavier| School
4 | Petrol | School
What will be the My Sql Query for it, Kindly help !!!
SELECT p.`id` AS "id" , p.`name`AS "Product Name",c.`category`AS "Category"
FROM `product` p
INNER JOIN `category`c ON (p.`cat_id`=c.`cat_id`)
ORDER BY p.`id` ASC
try this
SELECT A.id, A.name, B.category AS cat_name
FROM product A, category B
WHERE A.cat_id=B.cat_id

take data from mysql stored with "-" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My database is like this:
users table
_id_____favorites_____
1 | -53-87-96 |
2 | -12-54-87 |
images table
_id_____url____________
1 | smile.jpg |
2 | lol.jpg |
I stored favorites with seperator "-"; these favorites numbers are images id. When a user logs in they want to see their favorites. How can i write this query?
Favorites should be retrieved via a one-to-many relationship. The favorites table should look like
id | favorite
---+---------
1 | 53
1 | 87
1 | 96
2 | 12
2 | 54
2 | 87
Then you
select all from favorites where id = 1
Or whatever the id is.
Never, never, never store multiple values in one column!
That will give you serious problems, like in your case. You should change your database structure. You could add a favorite table
favorites table
+--------+------------+
|user_id |favorite_id |
| 1 | 53 |
| 1 | 87 |
| 1 | 96 |
+--------+------------+
Your current schema design is bad, you should properly normalize it into 3-table design.
User Table
UserID (PK)
UserName
other columns...
Images Table
ImageID (PK)
ImageLink
other columns...
Favorites Table
UserID
ImageID
other columns...
Anyway, to answer your question, MySQL's FIND_IN_SET will help but you need to replace - with , using REPLACE().
SELECT a.ID,
b.url
FROM users a
INNER JOIN images b
ON FIND_IN_SET(b.id, REPLACE(a.favorites, '-', ',')) > 0
WHERE a.ID = 1
SQLFiddle Demo