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
Related
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
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.
I have a table i want to merge in another one.
This table have entryID and activitiesID - the same entryID can have multiple activitiesIDs.
I want to move this data to another table which already have entryIDs
and an activitiesID empty column.
I would like to populate it:
each entryID to have only the last activitiesID from the first table.
Any suggestions?
thanks alot!
EDIT:
This code seems like is working for me:
UPDATE Individual
SET
Activities =(SELECT Indiv_status.activitiesID FROM Indiv_status WHERE
Individual.entryID=Indiv_status.IndividualID order by IndividualID desc
limit 1)
WHERE Individual.entryID = (SELECT Indiv_status.IndividualID
FROM Indiv_status
WHERE Individual.entryID = Indiv_status.IndividualID order by IndividualID desc limit 1);
Still need to check if the data comes correct but there is values from the second table now.
I'm trying to select a particular row in a database using SELECT and OFFSET. The result I get is logical and I do get the desired row I want. But then I need to UPDATE that same specific row so I do something like that:
UPDATE table
SET value=1
WHERE value IN (SELECT * FROM(
SELECT value FROM table WHERE some criteria LIMIT 1 OFFSET 2) temp_tab);
Now what I expect from this code is to UPDATE the selected row ONLY. Instead it Updates ALL rows in the datatable and sets their value to 1.
When I use only:
SELECT * FROM(
SELECT value FROM table WHERE some criteria LIMIT 1 OFFSET 2) temp_tab
I get only 1 row as the output. (LIMIT 1 OFFSET 2 makes sure I do get the 1 row and it's the 2nd available) I am not exactly sure what I am doing wrong or how I am supposed to achieve this.
Note: I do have to use SELECT and not some other method using unique ID of the row or something similar.
Any help would be greatly appreciated.
First, when using LIMIT and OFFSET you need to use ORDER BY as well. Otherwise the row you get is indeterminate.
One method uses LIMIT within the UPDATE itself. However, UPDATE doesn't allow OFFSET. So:
UPDATE table
SET value = 1
WHERE some criteria
ORDER BY ??
LIMIT 1;
The best method would use a unique id. You can do this with the double subquery approach:
UPDATE table
SET value = 1
WHERE id IN (SELECT id
FROM (SELECT id
FROM table
WHERE some criteria
ORDER BY ??
LIMIT 1 OFFSET 2
) t
);
If you don't have a single unique id, you can use multiple columns that uniquely define a single row.
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.