HI, i have two tables
1- name, id, code
2- id, value, concept
One name can have two concepts and two values. i want to retreive this:
Id, name, value1, value2.
How can i do that?
Tanks
Hello
I have a server with live databases, and I have a test-server where I sometimes restore databases from the live one, to get fresh data. But this way I have to set up all the users again, I have about 4 different users with different permissions that need set again.
Is there some way to do this a better way without using detach and attach, since I cant take the databases offline on the live-server?
Prefered some thing I could run that "re-fills" the tables with fresh data. No need to redo stored procedures and rights.
/M
I’m trying to design a database to use with ASP.net MVC application. Here is the scenario: There are three entities and users can post their comments for each of these different entities. I just wonder how just put one table for Comments and link all other entities to it. Obviously, Comments table needs 3 references (foreign key) to those tables but as you know these foreign keys can’t be null and just one of them can be filled for each row. Is there any better way than implementing three different tables for each entity’s comments?
I've two tables, shows and objects. I want to print out the latest objects, and the shownames for them. Right now I'm doing it this way:
SELECT MAX(objects.id) as max_id, shows.name, shows.id
FROM shows, objects
WHERE shows.id = objects.showId
GROUP BY shows.name
however, if I also want to fetch the episode of the object I can't put it like SELECT object.episode [...], because then wont automatically select the object which is MAX(objects.id), so my question is how to do that?
If you haven't already figured out my tables they're like this:
Shows
id
name
and also:
Objects
id
name
episode
season
showId
Using MySQL. Thanks!
Guys is there any better way to list all the months other than this
select to_char(add_months(to_date('01/01/1000', 'DD/MM/RRRR'), ind.l-1),
'MONTH') as
month_descr
, ind.l as month_ind
from dual descr
, (
select l
from (select level l from dual connect by level <=
12)
) ind
order by 2;
ANSWER :
SELECT to_char(add_months(SYSDATE, (LEVEL-1 )),'MONTH') as months FROM dual CONNECT BY LEVEL <= 12
Hi
I have 2 tables A and B with the following columns
Table A - id,bId,aName,aVal
Table B - id,bName
where A.bId is the same as B.id. I want a result set from a query to get
A.id, A.aName, B.bName where A.bId=B.id OR
A.id, A.aName, "" when A.bId=0.
In both cases, only those records should be considered where A.aVal LIKE "aVal"
Can someone please help me with the query? I can use left join but how do I get the blank string if bId=0 and B.bName otherwise?
Thanks
Hi I'm trying to output something like the following but am finding that there is a lot of code duplication going on.
| australian_has_itch | kiwi_has_itch |
| yes | no |
| no | n/a |
| n/a | no |
...
My query looks like this with two case statements that do the same thing but flip the country (my real query has 5 of these case statements):
SELECT
CASE
WHEN
NOT EXISTS (
SELECT person_id
FROM people_with_skin
WHERE people_with_skin.person_id = people.person_id
AND people.country = "Australia"
)
THEN 'N/A'
WHEN
EXISTS (
SELECT person_id
FROM itch_none_to_report
WHERE people.country = "Australia"
AND person_id = people.person_id
)
THEN 'None to report'
WHEN
EXISTS (
SELECT person_id
FROM itchy_people
WHERE people.country = "Australia"
AND person_id = people.person_id
)
THEN 'Yes'
ELSE 'No'
END australian_has_itch,
CASE
WHEN
NOT EXISTS (
SELECT person_id
FROM people_with_skin
WHERE people_with_skin.person_id = people.person_id
AND people.country = "NZ"
)
THEN 'N/A'
WHEN
EXISTS (
SELECT person_id
FROM itch_none_to_report
WHERE people.country = "NZ"
AND person_id = people.person_id
)
THEN 'None to report'
WHEN
EXISTS (
SELECT person_id
FROM itchy_people
WHERE people.country = "NZ"
AND person_id = people.person_id
)
THEN 'Yes'
ELSE 'No'
END kiwi_has_itch,
FROM people
Is there a way for me to condense this somehow and not have so much code duplication?
Thanks!
Asking a simple question, just want everyone have fun to solve it.
I got 2 tables.
1. Student
2. Course
Student
+----+--------+
| id | name |
+----+--------+
| 1 | User1 |
| 2 | User2 |
+----+--------+
Course
+----+------------+------------+
| id | student_id | course_name|
+----+------------+------------+
| 1 | 1 | English |
| 2 | 1 | Chinese |
| 3 | 2 | English |
| 4 | 2 | Japanese |
+----+------------+------------+
I would like to get the result all student, who have taken English and Chinese, NOT English or Chinese.
Expected result:
+----+------------+------------+
| id | student_id | course_name|
+----+------------+------------+
| 1 | 1 | English |
| 2 | 1 | Chinese |
+----+------------+------------+
What we normally do is
select * from student join course on (student.id = course.student_id) WHERE course_name = 'English' OR course_name = 'Chinese'
but in this result I can get User2 record which is not my expected result. I want the record only display the User take the course English+Chinese only.
hi guys,
I am getting result as decimal in storeprocedure.for eg:If iam getting result as 123.45
I want to split 123.45 to 123 and 45.Can any body help?
I'm trying to get the result of a COUNT as a column in my view.
Please see the below query for a demo of the kind of thing I want (this is just for demo purposes)
SELECT
ProductID,
Name,
Description,
Price,
(SELECT COUNT(*) FROM ord WHERE ord.ProductID = prod.ProductID) AS TotalNumberOfOrders
FROM tblProducts prod
LEFT JOIN tblOrders ord ON prod.ProductID = ord.ProductID
This obviously isn't working... but I was wondering what the correct way of doing this would be?
Hello,
i got a MySql DB.
There is a table with products and orders.
Structure:
Products: product_id, name, manufacturers_id
Orders: orders_id, product_id, quantitiy
Now I want to get all orders (show only products where product id=1).
I tried:
SELECT
orders.orders_id,
orders.product_od
FROM products, orders
WHERE products.manufacturers_id = 1
GROUP BY orders_id
ORDER BY orders_id
But this doesnt work
Hi
I need to SELECT INTO a temp table multiple times with a loop but I just can't do it, because after the table created by SELECT INTO you can't simply drop the table at the end of the loop, because you can't delete a table and create it again in the same batch.
so how can I delete a table in a stored procedure and create it again?
is it possible to this without using a temp table?
here is a snippet of where I am actualy using the temp table which is supposed to be a pivoting algorithm:
WHILE @offset<@NumDays BEGIN
SELECT
bg.*, j.ID, j.time, j.Status
INTO #TEMP1
FROM #TEMP2 AS bg
left outer join PersonSchedule j on bg.PersonID = j.PersonID and
bg.TimeSlotDateTime = j.TimeSlotDateTime and
j.TimeSlotDateTime = @StartDate + @offset
DROP TABLE #TEMP2;
SELECT * INTO #TEMP2 FROM #TEMP1
DROP TABLE #TEMP1
SET @offset = @offset + 1
END
I haven't spent a ton of time researching this yet, mostly looking for best practices on upgrading/changing DB schemas.
We're actively developing a new product and as such we often have additions or changes to our DB schema. We also have many copies of the DB -- one for the test environment, one for the prod environment, dev environments, you name it. We don't really want to have to blow away test data every time we want to make a change to the DB.
Are there good ways of automating this or handling this? None of us have really ever had to deal with this so...
I'm using a MySQL database and accessing it through Java.
PreparedStatement prep1 = this.connection.prepareStatement("UPDATE user_table
SET Level = 'Super'
WHERE Username = ?");
prep1.setString(1, username);
The update statement above works fine however I'd like to get the number of rows affected with this statement. Is this possible please?
I have a problem parsing a stored procedure parameter in the form:
declare @S varchar(100)
set @S = '4=2,24=1534'
Here's the query:
select
cast(idx as varchar(100)) 'idx'
, value
, SUBSTRING(value, 1, charindex(value, '=')+1) 'first'
, SUBSTRING(value, charindex(value, '=')+1, LEN(value)-charindex(value, '=')-1) 'second'
from Common.SplitToTable(@S, ',') -- returns (idx int, value varchar(max))
where len(value) > 0
But here is the result I get:
idx value first second
0 4=2 4 4=
1 24=1534 2 24=153
Here's what I expected:
idx value first second
0 4=2 4 2
1 24=1534 2 1534
Help?
Hello, I have a table with measures and the time this measures have been taken in the following form: MM/DD/YYYY HH:MI:SS AM. I have measures over many days starting at the same time every day.The datas are minute by minute so basically the seconds are always = 0. I want to select only the measures for the first 5 minutes of each day. I would have used the where statement but the condition would only be on the minutes and note the date is there a way to do this?
Thanks
Hi I'm having a production database and its replicated report database. How to shrink the transaction log files in the production database as the log file size is increasing. I had tried DBCC SHRINKFILE and SHRINKDATABASE commands but it does not work for me. I can't detach and shrink and attach back as the db in replication. Please help me in this issue.
Say I have the simple table below:
KeyWordID KeyWord
----------- ----------
1 Blue
3 Yellow
1 Yellow
How would I select the KeyWordID that selects the KeyWordIDs that where both KeyWord is Blue and Yellow.
E.g. it should only return 1, as this is the only KeyWordID that has both Keywords Blue and Yellow
I initially thought GROUPBY - but its not quite working as expected.
Hello,
My question is I have a table with the set of records. I am calling a stored procedure for some other purpose. But when ever it finds some duplicate records. It need to return as error message back to php.
C1 C2 c3
abc 32 21.03.2010
def 35 04.04.2010
pqr 45 30.03.2010
abc 12 04.05.2010
xyz 56 01.03.2010
ghi 21 06.05.2010
def 47 17.02.2010
klm 93 04.03.2010
xyz 11 01.03.2010
For the above set it need to check for the records that has the same c1.
The stored procedure should return as abc,def,xyz are duplicate.
I tried something like this. This will not work it has more than 1 set of duplicate records. Please help me to enhance this to solve the purpose.
SET @duplicate = (SELECT c1 FROM temp GROUP BY c1 HAVING count(c1) > 1)
--Check for duplicate concession Nr.
IF(len(@duplicate) > '1')
BEGIN
SET @error = @error + ' Duplicate C1 Number:- ' + @duplicate
SET @errorcount = @errorcount + 1
END
As this one type error I am checking for errorcount.
IF @errorcount <> '0'
BEGIN
GOTO E_General_Error
END
-- If an error occurs, rollback and exit
E_General_Error:
PRINT 'Error'
SET @error = @error
IF @@error <> 0 SET @error = 'Database update failed'
ROLLBACK TRANSACTION update_database
RETURN
END
Now it is able to return Duplicate c1 number abc. If there are more than 1 problem comes.
Thanks in advance!
HI !
This is my table:
CREATE TABLE [ORG].[MyTable](
..
[my_column2] UNIQUEIDENTIFIER NOT NULL CONSTRAINT FK_C1 REFERENCES ORG.MyTable2 (my_column2),
[my_column3] INT NOT NULL CONSTRAINT FK_C2 REFERENCES ORG.MyTable3 (my_column3)
..
)
I've written this constraint to assure that combination my_column2 and my_column3 is always unique.
ALTER TABLE [ORG].[MyTable] ADD
CONSTRAINT UQ_MyConstraint UNIQUE NONCLUSTERED
(
my_column2,
my_column3
)
But then suddenly.. The DB stopped responding.. there is a lock or something..
Do you have any idea why?
What is bad with the constraint?
Hi, this is what I want to achieve:
4 tables are involved:
Players with PlayerID as PK,
Competitions with CompetID as PK
Results with ResultID as PK and CompetID as FK
And the 4th table: PlayerResultts with ResultID + PlayerID as PK and CompetID as new column I created.
Competitions, results and PlayerResults are already populated and quite large (300000 PlayerResults so far).
In order to populate the PlayerResults.CompetID column, I try a Update ... (Select....) request but I'm not aware of the right syntax and it fails.
Here is my feeble attempt:
update PlayerResults
set competid = (select distinct(r.competid) from results r, playerresults p
where r.resultID = p.resultid)
Error is (of course):
"Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , , = or when the subquery is used as an expression."
Can someone put me in the right direction? TIA
So I have a query, can someone let me know if it looks ok content wise?
"INSERT INTO ".TBL_MESSAGES." (NULL, 'Your ranking points have changed',
'Due to your recent activity, your ranking points have increased by $r', '2', '$u',
'0', '0', '0', '0', NULL, NULL, NULL, NULL, now())";
I can add further information if the query doesnt appear to have a problem?
Thanks
I've developed a form that inserts many things into the database. But somehow, when the page is filled up; it inserts only the user_password that too of the database admin.
here is the code. Any help would be great.
Invalid Name: We want names with more than 3 letters.
Invalid E-mail: Type a valid e-mail please.
Passwords are invalid: Passwords doesnt match or are invalid!
Please enter your contact number.
Please enter your age
Congratulations! All fields are OK ;)