Get necessary output from view - mysql

I am trying to query from view to remove the row that has null values on column value if only the column name has duplicate row count.
So here is my sample result from my view and what I expect is the highlighted row should not be in the result, just want to have recommendation and suggestion how can I easily remove that highlighted row using query because right now I am removing through my application code.
Select * from TblView1

If I understand the question correctly, you want to:
Include all rows with a unique Name
Include rows without null for rows with non-unique Names
I believe this should work:
select * from TblView1 where Name in
(select Name from TblView1 group by Name having count(*) = 1)
or location is not null

Related

SQL GROUP_CONCAT query assistance

I am having some problems figuring out the best use of GROUP_CONCAT. In the query below I am trying to select multiple images from JAM_Plot_Images and set them against single records. So get record 1, display image1.jpg, image2.jpg in each row. Then get record 2, display image3.jpg, image4.jpg in the 2nd row and so on.
SELECT *, GROUP_CONCAT(JAM_Plot_Images.image ORDER BY JAM_Plot_Images.image) AS images
FROM JAM_Plots
LEFT JOIN JAM_Plot_Images ON JAM_Plots.page_id = JAM_Plot_Images.page_id
GROUP BY JAM_Plots.page_id
The problem I have is if a row has no images in a row it breaks the unique identifier when outputting the records, but only for that record. So if records 1 2 and 4 have images it will output everything fine, but if record 3 has no image the unique ID won't appear. NULL values appear within phpmyadmin output. I have tried to use COALESCE to fix the issue but can't quite get it to work.
This problem has nothing to do with GROUP_CONCAT(), it's a general problem with LEFT JOIN when you have duplicate column names between tables.
Since you have the same column name page_id in both tables, both of them will be selected in the results. But when there's no images, JAM_Plot_Images.page_id will be NULL.
To disambiguate them, you should give an alias to JAM_Plots.page_id, and use that.
SELECT *, JAM_Plots.page_id AS jp_page_id, GROUP_CONCAT(...)
...

Why is my SQL Query returning multiple rows?

I have two tables and I am trying to retrieve the thread-id corresponding to the max(id) (which is incremented) of the first table and the user id of the second table. In the sql statement, I have joined the tables and tried to retrieve the column, but am receiving multiple rows and I am not sure why. I put an example user ID of 1 and I am expecting a retrieval of only one row, which should have a value of 85.
In spelled out words, I want to get the thread-id from Table 1 that corresponds to the user_id of Table 2 and the maximum id of Table 1.
I have the fiddle below.
http://sqlfiddle.com/#!9/8a7d5d/56
Give this a try:
SELECT
$table_messages.thread_id
FROM
$table_messages
RIGHT JOIN
$table_recipients ON $table_messages.thread_id = $table_recipients.thread_id
WHERE
user_id = 1
order by $table_messages.id DESC
Limit 0,1;
In your last line you have the following SELECT statement:
(SELECT MAX($table_messages.id))
However, you have not specified which table to get this value from. Change this statement to:
(SELECT MAX($table_messages.id) FROM $table_messages)
This should now return one row.

Selecting Distinct rows When there is a Logical OR with two columns

As the title suggests I have a MySQL query like this:
SELECT DISTINCT `friendly_url` FROM `post` WHERE `description` LIKE ? OR `heading` LIKE ? ORDER BY `friendly_url`
I have given the string '%' wild card in the parameters so that this works as a Search function. How ever, Say a user is searching for a common word like 'is' and it appears in both heading and description in the same post. Then this query returns the same post twice. I don't want that to happen, hence the 'DISTINCT'.
Why does this happen? Any way I can work around to make it work the way i want?
The query is not returning the same row twice. The predicates in the WHERE clause are evaulated against each row, and either the row is returned, or it's not. No combination of OR conditions is going to cause MySQL to return the "same row" multiple times.
If you are getting duplicate values of friendly_url, then you have multiple rows in the post table that have the same value for friendly_url. (i.e. friendly_url column is not unique in the post table.)
You don't have to use the DISTINCT keyword to remove duplicates from the resultset. You could remove the DISTINCT keyword, and add GROUP BY friendly_url before the ORDER BY clause.
To identify the "duplicated" values of friendly_url, and how many rows have that same value:
SELECT p.friendly_url
, COUNT(1)
FROM post p
GROUP BY p.friendly_url
HAVING COUNT(1) > 1

How to get specific cell from table mysql to Vb.net

I need to get a specific data/cell from a table and embed it to a label.text, for example
I have a table named Student with columns of Student_No., Student_Name, Student_Age, Year and Section. And I need to get a single data/cell from a column but the next code will get the one next to it and so on.
I got a code like this:
SELECT col_name
FROM table_name
WHERE col_name='values'
LIMIT 1
but it gets the first one and if I change the limit to 2 it gets two...
you should specify the id of the entry you want to get from the table
SELECT col_name
FROM table_name
WHERE col_name='values'
AND id = '1' LIMIT 1

How can I select the second last and last row from the database?

When we are retrieving data from database, we use something like this
SELECT * FROM Names
However, we will get all the data inside the specific table. Since I am going to update some data to the database and want to make a comparison bewteen the last row of data in the db and the most updated data, how can I select and retrieve the last two row of the database only?
If you were using SQL Server, you would do something like this:
SELECT TOP 2 * FROM Names ORDER BY Name DESC
SQL Server syntax:
SELECT TOP 2 column_name FROM table_name ORDER BY column_name DESC;
Example:
If you want to retrieve the last value of the customer_name column from the customers table, you need to write the following query:
SELECT LAST (CUSTOMER_NAME) AS LAST_CUSTOMER FROM CUSTOMERS;
You should include a column in the Names table to keep track of when a name was added to the table since you cannot guarantee that the rows are in the order that they were inserted. With that column you can use the order by clause..
In MySQL Syntax:
SELECT *
FROM Names
ORDER BY order_column DESC
LIMIT 2;
If you want to get the last rows as they are, you cannot order the table by the inserted names because that is just getting the 2 names that come last in an alphabetically sorted list of names. You could try something like this where you include an offset in the limit clause if you get the number of rows in the table:
SELECT *
FROM Names
LIMIT *num_rows*-2, 2;
You would have to know the number of rows to use this query or use an additional query to implement a row count that works with limit. This, however, still may not be accurate since the system may not order the rows as they were inserted. I still recommend the first option where you keep track of order/time a name was inserted.