how to join these tables properly in sqlite3 [closed] - database

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 1 year ago.
Improve this question
I have 3 tables. First there is Posts tables which looks like this
id
post_creator
1
Mark
2
John
3
David
4
Ellie
5
Thomas
6
Elliot
There are 2 types of post. image post and text post. I have 2 different tables for them. Here's how text_posts table look like:
post_id
post_text
1
lorem ipsum
4
lorem ipsum
5
lorem ipsum
again there's an image_posts table both follows id from posts table
post_id
post_text
post_image
2
lorem ipsum
image_url
3
lorem ipsum
image_url
6
lorem ipsum
image_url
Now I dont care about post_image urls, all I want is to get post_creator from Post table along with post whether its in text_post table or image_post table I want the texts. How can I do that?

I assume that you are using a SQL Db.
In this case, Posts and post_text/image_posts are a NtoN relationship so you should normalize the relationship as 2 1toN.
In this case it ends with 1 table Post, 2 Table: Post_text and Post_img and another table that maintains the "history" of post per each user.
You should create another table named Creates(id, user_id, post_id).
Then you can join the 2 table and obtain the post_creator.
SELECT
post_id, post_text, post_image
FROM Posts
LEFT JOIN text_posts ON text_posts.post_id = Posts.id
LEFT JOIN image_posts ON image_posts.post_id = Posts.id
Then you will obtain a table having all the field of the tables as result. To understand if the post is an image one just check the post_image field. If it is != null then it would be an image_post.
However, the structure you used is very bad, I suggest you to study a bit better how to structure a SQL table and Db and try to re-structure your data in a better way, following the Normalization rules.

Related

How to order by multiple columns with same datatype [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 last year.
Improve this question
I want to sort multiple columns at the same time meaning I do not want to sort first column then second column which sql server order by generally does. Below is the example of how I want the result should be. Note that all column have same datatype.Also, Note that one of the columns in a row only have data.
I have a table that have values
FirstName
LastName
MiddleName
matt
jeff
dave
TomBrady
zedaya
Mccafrey
tom
I want to sort this table by all the values in the three columns such that rows should populate as per the ascending or descending order of each value in the row.
Output should be:
FirstName
LastName
MiddleName
dave
jeff
matt
Mccafrey
tom
TomBrady
zeday
I have 3 columns in which each row will have only of value of the three columns. I need to sort all the rows irrespective of the column (basically consider all the values appear in a single column, and sort). In the example above, dave is lowest according to sort order, so that row should come first. following by jeff row, followed by Maccafrey, Matt, Tom, TomBrady, Zedaya.
The right solution has been ticked as correct. Used Contact to order by all the fields at the same time.
Try converting the blanks to ascii values that are last in the table and sorting, does this work for you?
select *
from t
order by
IsNull(NullIf(firstname,''),Char(255)),
IsNull(NullIf(lastname,''),Char(255)),
IsNull(NullIf(middlename,''),Char(255))
Example Fiddle
Your sample data was ambiguous and misleading. If you really just want to order by whatever column isn't '', and given your assurance that no columns are NULL and there are no ' ' etc., you can use CONCAT, which will simply munge whatever value is not empty with two empty strings:
SELECT FirstName, LastName, MiddleName
FROM dbo.t
ORDER BY CONCAT(firstname, lastname, middlename);
Output:
FirstName
LastName
MiddleName
dave
jeff
matt
Mccafrey
tom
TomBrady
zedaya
example db<>fiddle

After inserting one row for Ukrainian Language, the field in app page is showing ????? even after removing the row [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 1 year ago.
Improve this question
There is a TEXT_TRANS table which contains the translation of a specific word and they contain same textid but different languageid. Suppose there is a row in TEXT_TRANS table for the Name column 'Mohan' which has a specific textid and languageid . For Ukrainian language there will be another row for Name column 'мохан' which will have same textid as of english but different languageid. So I have inserted one row for Ukrainian language but forgot to put N before ukrainian word because it will show ?????. Then I updated correctly by putting N before the ukrainian word. However, The field is still showing as ?????? even when I deleted that row for Ukrainian language.
To store and select Unicode character in database you have to use NVARCHAR instead of VARCHAR. You need to be sure that column has correct datatype.

Parameter value depending on another parameter in reports [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 1 year ago.
Improve this question
i have a dropdown as parameter in my report which contain 3 values: value1 value2 and none. on clicking on value1 i want another parameter to appear as below:
enter image description here
If i click on none then nothing should display. Is it possible to achieve this by using filter/dataset? i am not sure how to do it. Any help will be really appreciated. Thanks
You cannot change the names or captions of the parameter drop down control at run time. So you cannot have the second parameter labelled as 'Value 1' if 'Value 1 was selected in your first parameter.
You can however, control what is shown in your second parameter and have the list of items be dependent on your first parameter value.
For example, lets say we had a table called products that contained the following..
ProdID
Category
Colour
Location
1
Bike
Red
A
2
Bike
Blue
A
3
Bike
Green
A
4
Car
Red
A
5
Car
Red
B
6
Car
Blue
B
7
Boat
Red
A
8
Boat
White
A
9
Boat
Blue
A
If your first parameter was called p1 had the following value, caption pairs
1 = Category
2 = Colour
3 = Location
Then the dataset query for the seconds parameter would be something like
SELECT DISTINCT
CASE #p1
WHEN 1 THEN [Category]
WHEN 2 THEN [Colour]
WHEN 3 THEN [Location]
END as pValues
FROM [products]
ORDER BY 1;
If this does not help, please edit your question and explain exactly what you are trying to achieve.

Need help for DB structure

In my application user can post questions and comments. each question has many comments and a comment is belongs to a question. This application may have millions of users.we have to extract tags from the questions and comments.
Now we planned to have db structure like this.
questions table(id,question)
comments table(id,comment)
tags table (id,tag_name)
tags_questions_comments (id,tag_id,question_id,comment_id)
Now I am having a confusion whether this is correct or not ?.
id tag_id question_id comment_id
--------------------------------------
1 1 1 NULL -> tag from question
2 2 1 NULL
3 3 1 1 -> tag from comment
Thanks
I would make two tables instead of:
tags_questions_comments (id,tag_id,question_id,comment_id)
The one includes the set:
tags_questions (tag_id,question_id)
And another one for the the comments:
questions_comments (question_id,comment_id)
unless you add a questionId columns to the comments table which could be a better approach according to your needs.

Counting Number of records in each row in SQL Server [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 8 years ago.
Improve this question
Again I m sry fr that non-sense way of asking the question what i wanted to say that at the last column some rows have null values and some rows have a value which is not necessary.. So what i wanted to do to delete those rows which have not null values in last column and then deleting the whole last column...
The Table looks like this..:
Col1 Col2 Col3 Col4
A A A A
B B B
C C C
D D D D
So here i want to keep the row 2 and 3 and delete the row 1 and 4..Then deleting the whole column 4...
My answer is based on your comment:
Oopss!!! i am extremely sorry fr making a mess..:( what i wanted to
say that at the last column some rows have null values and some rows
have a value which is not necessary.. So what i wanted to do to delete
those rows which have not null values in last column and then deleting
the whole last column..
Here it is:
Delete from dbo.YourTable Where Last_Column IS NOT NULL
After that, dropping the column is easy if you go to table right click -> design
If it doesn't let you drop the column due to an error, you should do this:
Tools -> Options -> Designers-> Uncheck "Prevent saving changes that require table re-creation"
I'd suggest that you undo that change after you're done though.
Best of luck =)
What you're trying to do won't work because they'll all have the same number of columns.
So don't waste your time trying to implement it because it won't solve whatever problem you're trying to solve!
DELETE dbo.table WHERE Col4 IS NOT NULL;

Resources